When set command is used without any options, it displays the names and values of all shell variables and functions.
We want to display only the variables and avoid the functions from the output.
When set command is used without any options, it displays the names and values of all shell variables and functions.
We want to display only the variables and avoid the functions from the output.
As I already stated in a comment, env
doesn't fit the requirement as it only shows exported variables.
Processing set
output to filter out anything that doesn't look like a variable definition is an unreliable hack. You will in particular miss a part of variables which value contains a line feed. Even worst, a carefully written function can make appear fake variables definitions.
The simplest solution is to switch temporarily to the POSIX mode where functions are not considered to be (kind of) variables:
set -o posix
set
set +o posix
There is however an issue if your default mode is already POSIX, or if you want that command to work whatever POSIX mode the shell is set to.
In such case, here is a workaround:
(set -o posix;set)
This only set the POSIX mode for the set
builtin executed in a subshell and the parent shell mode stay unaffected.
set | grep -E '^\S+=\S'
....right @terdon?
– frp farhan
Dec 06 '16 at 12:45
set
outputs multiline variables as foobar=$'foo\nbar\nbaz'
on a single line. Which is easier to parse then.
– Antoine Pinsard
Oct 14 '17 at 09:03
While the simplest solution is to use env
instead of set
, env
doesn't give all existing variables but only those that would be passed to any process started with env
(so no unexported variables, for example). Another approach is to search the output of set
for lines that have a string of non-whitespace, an =
and then another string of non-whitespace characters:
set | grep -E '^\S+=\S'
However, this will miss any variables set to a multiline value (like IFS
whose default value includes a \n
).
set
doesn't print functions. At least not on my system. What kind of fake variables are you thinking of?
– terdon
Dec 06 '16 at 18:32
set
builtin print functions, that's the whole point of the OP question. Here is an example of such a fake variable: f() { a="a[ENTER]fubar=/tmp/gotcha[ENTER]b"; }
. In fact, the same trick might also be used with multiline variables.
– jlliagre
Dec 06 '16 at 20:32
set
and grepping for it, and I failed to find it. Now I'm on a different system, tested again and did find it so I'm back to thinking you were absolutely correct in the first place. I'll go back to work tomorrow and try it again. I probably just did something stupid but in my defense, I did test!
– terdon
Dec 06 '16 at 20:53
set
output because you might have been running bash in POSIX mode, or running dash, ksh or whatever shell variant.
– jlliagre
Dec 06 '16 at 22:25
For some odd reason, declare -p
seems to list only variables:
$ declare -p
declare -x ANT_HOME="/usr/share/apache-ant"
declare -- BASH="/usr/bin/bash"
declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath"
declare -ir BASHPID
declare -A BASH_ALIASES=()
declare -a BASH_ARGC=()
declare -a BASH_ARGV=()
declare -A BASH_CMDS=()
...
Of course, this has the disadvantage that it outputs declare
commands that can recreate the variable with all its attributes. Depending on what you intend to do with the output, that might actually be useful.
I'm not sure, but the cow would consider something.
_____
( env )
-----
o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||
In other words, just use env
instead.
I guess she's just read jilliagre's comment.
________________________________
/ But it will only show exported \
\ variables! /
--------------------------------
\ ^__^
\ (!!)\_______
(__)\ )\/\
!! ||----w |
|| ||
env
instead of set
. It's a command that prints out environment variables.
– terdon
Dec 06 '16 at 11:30
env
prints only the global environment variables, however set
without any options prints LOCAL and GLOBAL shell variables and functions.So both ain't same right?
– frp farhan
Dec 06 '16 at 11:32
set
.
– terdon
Dec 06 '16 at 11:38