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?
set outputs multiline variables as foobar=$'foo\nbar\nbaz' on a single line. Which is easier to parse then.
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?
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.
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 will only show exported variables.
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?