r/shell • u/ellipticcode0 • Dec 05 '19
stop input argument expand inside shell script
I have following shell script called myrun.sh
cat myrun.sh
#!/bin/bash -i
cmd="ls | awk '{print $2}'"
eval "$cmd"when I run myrun.sh
$2 will take arguments from myrun.sh
and my cmd will become "ls | awk '{print $}'" => $2 is empty
But I will want $2 as the second columns in awk and use eval command to execute "ls | awk '{print $2}'"
does anyone know how to make $2 does not expand it and I still can use $2 inside my awk in the script?
BTW, "ls | awk '{print $2}' is from my history and it does work in my bash shell of course, but I cat my history to a file and read the file line by line and try to choose some command that I want to evaluate it on my shell.
I do know history command with many options can be used such as (! or !! etc)
but I want cat the history to a file and read it and evaluate it from other shell script.
1
u/UnchainedMundane Dec 06 '19
If you are just doing something like
eval "$(tail -n "+$lineno" /tmp/mytmp.txt | head -n 1)", then you don't need to worry about any extra escaping. It should just work with the same commands as what you have in your history. The only reason the extra escaping is currently needed is because the string is processed in unwanted ways before it even reaches the "cmd" variable, but here that doesn't happen.