The shell's -c
option expects the command line as one argument, and $@
splits ls -l
into two. (So would "$@"
, just that it would keep argument strings with whitespace intact, so it doesn't directly
help either.)
So, assuming you want to pass an actual command line, you'll have to either pass just a single string to the script, and pass it on with sh -c "$1"
, or if you like, join all arguments to the script into one string, and then use sh -c "$*"
, i.e.
#!/bin/bash
setarch ... /bin/bash -c "$*"
See:
This would support including all shell syntax in the command line, e.g.
tempenv 'ls -l; echo $SOMEVAR'
tempenv '[ some test ] && echo true'
But on the other hand, arguments that are supposed to contain whitespace, e.g. file names with such, need to be quoted twice, e.g.
tempenv 'ls -l "Some Document.txt"'
$@
, see my answer. – Hauke Laging Nov 26 '21 at 00:31