Commands tagged environment (13)

  • Print environment (system) information using Perl.


    3
    perl -e 'print map { $_ .= "$ENV{$_}\n" } (keys %ENV)'
    MarxBro · 2012-10-13 23:38:28 6
  • This uses some tricks I found while reading the bash man page to enumerate and display all the current environment variables, including those not listed by the 'env' command which according to the bash docs are more for internal use by BASH. The main trick is the way bash will list all environment variable names when performing expansion on ${!A*}. Then the eval builtin makes it work in a loop. I created a function for this and use it instead of env. (by aliasing env). This is the function that given any parameters lists the variables that start with it. So 'aae B' would list all env variables starting wit B. And 'aae {A..Z} {a..z}' would list all variables starting with any letter of the alphabet. And 'aae TERM' would list all variables starting with TERM. aae(){ local __a __i __z;for __a in "$@";do __z=\${!${__a}*};for __i in `eval echo "${__z}"`;do echo -e "$__i: ${!__i}";done;done; } And my printenv replacement is: alias env='aae {A..Z} {a..z} "_"|sort|cat -v 2>&1 | sed "s/\\^\\[/\\\\033/g"' From: http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    2
    for _a in {A..Z} {a..z};do _z=\${!${_a}*};for _i in `eval echo "${_z}"`;do echo -e "$_i: ${!_i}";done;done|cat -Tsv
    AskApache · 2010-10-27 07:16:54 5
  • Grabs the cmdline used to execute the process, and the environment that the process is being run under. This is much different than the 'env' command, which only lists the environment for the shell. This is very useful (to me at least) to debug various processes on my server. For example, this lets me see the environment that my apache, mysqld, bind, and other server processes have. Here's a function I use: aa_ps_all () { ( cd /proc && command ps -A -opid= | xargs -I'{}' sh -c 'test $PPID -ne {}&&test -r {}/cmdline&&echo -e "\n[{}]"&&tr -s "\000" " "<{}/cmdline&&echo&&tr -s "\000\033" "\nE"<{}/environ|sort&&cat {}/limits' ); } From my .bash_profile at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    1
    cd /proc&&ps a -opid=|xargs -I+ sh -c '[[ $PPID -ne + ]]&&echo -e "\n[+]"&&tr -s "\000" " "<+/cmdline&&echo&&tr -s "\000\033" "\nE"<+/environ|sort'
    AskApache · 2010-10-22 02:34:33 14
  • Same as previous but compatible with BSD/IPSO


    1
    ps ewwo command PID | tr ' ' '\n' | grep \=
    egreSS · 2011-09-15 12:07:46 3
  • Normally the bash builtin command 'set' displays all vars and functions. This just shows the vars. Useful if you want to see different output then env or declare or export. Alias 'sete' shows sets variables alias sete='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/q;p"' Alias setf shows the functions. alias setf='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/,\$p"' Also see: http://www.commandlinefu.com/commands/view/6899/print-all-environment-variables-including-hidden-ones At the very least, some cool sed commands! From my .bash_profile http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    0
    alias sete='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/q;p"'
    AskApache · 2010-11-17 23:58:01 10
  • Python virtual environment creation.


    0
    $sudo aptitude install python-virtualenv; virtualenv --no-site-packages jpaenv; source jpaenv/bin/activate
    moulip · 2011-04-21 08:28:18 3
  • This exports all lines of input file as environment variables, assuming each line is like these: OH=YEAH FU=UUUU


    0
    while read line; do export $line; done < <(cat input)
    dario · 2013-03-15 08:14:04 10
  • Sometimes there are just no variables such as $DESKTOP_SESSION, $GDMSESSION, or $WINDOWMANAGER. Show Sample Output


    0
    ls -l /usr/share/xsessions/
    puresky · 2014-01-17 05:09:18 7
  • Normally executing 'set' returns a vast amount of information, including the source code of every function and variable within the environment - including those that are part of the shell. By using the -o posix argument, bash runs temporarily in POSIX mode for this command, which simplifies expressions and leaves out the shell's own functions and definitions - leaving a much smaller, more useful list. Show Sample Output


    0
    alias allvars=' ( set -o posix; set ) | less'
    incidentnormal · 2016-03-04 14:04:45 12

  • 0
    eval "unset $(printenv | grep -ioP '(?:https?|no)_proxy' | tr '\n' ' ')"
    acavagni · 2019-06-28 10:40:41 35
  • This function is used to set environmental variables from a list of alternatives depending on what's installed on the system. It returns the first program found in the list. Example usage: export BROWSER=$(find_alternatives chromium-browser google-chrome opera firefox firefox-bin iceweasel konqueror w3m lynx) . export EDITOR=$(find_alternatives vim nano pico emacs kate) . export PAGER=$(find_alternatives vimpager less most more pg)


    -1
    find_alternatives(){ for i;do which "$i" >/dev/null && { echo "$i"; return 0;};done;return 1;}
    eightmillion · 2011-01-06 19:53:46 3

  • -1
    printenv
    tatwright · 2012-10-14 17:47:18 4

  • -1
    perl -e 'print "$_=$ENV{$_}\n" for keys %ENV'
    sgikas · 2012-11-12 13:14:28 4

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Numerically sorted human readable disk usage
Provides numerically sorted human readable du output. I so wish there was just a du flag for this.

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Split huge file into DVD+R size chunks for burning
Real DVD+R size is 4700372992 bytes, but I round down a little to be safe. To reconstitute use cat. "cat file.img.gz.aa file.img.gz.ab ..... > file.img.gz"

Find the package that installed a command

Delete all but the latest 5 files, ignoring directories

cat stdout of multiple commands
Concatenate the stdout of multiple commands.

check open ports without netstat or lsof

Check command history, but avoid running it
!whatever will search your command history and execute the first command that matches 'whatever'. If you don't feel safe doing this put :p on the end to print without executing. Recommended when running as superuser.

Convert file type to unix utf-8
converts encoding of a file to unix utf-8 useful for data files that contain what would be usable ascii text but are encoded as mpeg or some other encoding that prevents you from doing common manipulations like 'sed'

check open ports without netstat or lsof


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: