Michael Goldshteyn
Michael Goldshteyn

Reputation: 74450

Is it possible to simplify this BASH eval expression?

Given the following Bash shell script excerpt:

# The intent is to take the PATH env variable, break it up into its parts, making them
# appear to be command line args (i.e., `$1`, `$2`, ...), and then for this example, just
# echo the parts in space delimited form, but we can imagine that we may want to do other
# things with them - this is just sample usage

# Important Requirement/Constraint
# ================================
# Please do not alter the "PATH to $1, $2, $3, ..." portion of the answer or replace the
# Bash ".." range construct with the output of the "seq" command exec'd in a subshell.
# Preferably, the answer should simply consist of the simplification of the last line of
# code - the "eval eval ..." . Also, please don't simplify by collapsing the whole thing
# to just echo "$@" since we may want to work with only some of the parts, and not
# necessarily the first parts, of the path. That is to say that the 1 and $# in the
# {1..$#} range could be replaced with other shell variables or expr., potentially

# Test case
PATH=/usr/local/bin:/usr/bin:/bin

# The code being examined follows

# Set ':' as the input field separator of the path
IFS=: # Or, more appropriately if in a function: local IFS=:

# Parse the PATH environment variable and break it up into its components
set $PATH

# This is the line we want to simplify, if possible, without losing functionality of
# course (see the comment that follows for details)
eval eval echo '\'$(eval 'echo "\${1..$#}"')

# Some notes and explanations regarding the functionality and underlying intent of the
# preceding line:
#   - We start by dynamically creating the following construct: ${1..3}
#     since $# is 3 for our example
#   - Use Bash to expand that construct to: $1 $2 $3
#     these vars contain the parsed parts of the PATH
#   - Finally, display the three parts of the PATH using echo: echo $1 $2 $3
#   - This causes the following text to be sent to STDOUT:
#     /usr/local/bin /usr/bin /bin

So, can the eval eval... line in the preceding code be simplified, but still produce the desired output, which for the above example is:

/usr/local/bin /usr/bin /bin

I am thinking along the lines of a solution that would replace some of the echo commands with input/output redirection (perhaps) or maybe a reordering/collapsing of sorts that would lead to the need for fewer eval commands than are used in the example.

Upvotes: 0

Views: 131

Answers (3)

KamilCuk
KamilCuk

Reputation: 141881

but still produce the desired output,
/usr/local/bin /usr/bin /bin

Just:

echo "${PATH//:/ }"

The intent is to take the PATH env variable, break it up into its parts, making them appear to be command line args (i.e., $1, $2, ...), and then for this example, just echo the parts in space delimited form, but we can imagine that we may want to do other things with them - this is just sample usage

I do not trust unquoted shell expansions.

IFS=':' read -ra patharr <<<"$PATH"
set -- "${patharr[@]}"
IFS=' '; printf "%s\n" "${patharr[*]}"

Upvotes: 1

Walter A
Walter A

Reputation: 20032

Sticking close to the original, you can do

IFS=:
set $PATH
echo "$@"

If you don't want to change IFS and PATH, you can do

set $(sed 's/[^=]*=//;s/:/ /g' <<< ${PATH})
echo "$@"

Upvotes: 1

petrus4
petrus4

Reputation: 614

echo "${PATH}" | tr ':' '\n' > stack

count=1

echo "#/bin/sh-" | tr '-' '\n' >> stack2 

while read line 
do
echo "path${count}=${line}" >> stack2
count=$(($count+1))
done < stack

source stack2

Now you've got every section of the path, in its' own named variable.

Upvotes: 1

Related Questions