This is a simplified version of a script that runs with one parameter
#!/bin/bash
K1=eval /usr/bin/urlencode "$1"
echo "$K1" # prints the correct url encoded $1 parameter
echo "$K1" # nothing printed
I looked at other more complex questions/answers to get why the variable lose its content
Moreover, without echoing $K1
anytime, I lose its value if I try to use it in another value like K2="zgrep $K1$DIR
. Echoing $K2
prints zgrep $DIR
value
As running this with bash -x
will readily reveal, the echo
outputs the variable's value both times. You are assigning K1="eval"
for the duration of the urelencode
command, and its results are simply being displayed on standard output. Presumably K1
was unset, so it then returns to an empty string; you are then running echo ""
twice.
I'm guessing you were trying to say
k1=$(urlencode "$1")
echo "$k1"
echo "$k1"
This is a very common beginner problem. See also How do I set a variable to the output of a command in Bash?, Assignment of variables with space after the (=) sign?, and somewhat more tangentially Correct Bash and shell script variable capitalization
/usr/bin
should already be on your PATH
so I can't see a good reason to provide a full path to the executable. If you genuinely have a different tool with the same name earlier on your PATH
, probably sort out that problem instead.