How to remove the functions displayed by set command?

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.


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).


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.

Tags:

Shell

Bash

Set