I have the following code:
set -o xtrace
openDirectory ()
{
lxterminal --command="zsh -c 'cd "$1"; zsh -i' "
}
# Handle argument.
if [ "$@" ]
then
openDirectory ~/Projects/Clients/"@1"
cd $1
fi
This fails if there is a space in the argument passed.
lxterminal '--command=zsh -c '\''cd /home/chris/Projects/Clients/Test' - 'Space; zsh -i'\'' '
cd Test - Space
cd: too many arguments
How can I properly escape this? Is this even feasible to be done with something like bash?
Assuming the enclosing shell really is bash (as the question is tagged), you can use
printf -v varname %q "$var"
to store a safely quoted instance of the value ofvar
into the variable namedvarname
, as such:printf '%q'
evaluates to a version of the given string which will, wheneval
'ed by bash (or any shell with equivalent semantics), evaluate to the original literal value.To explain why this is done that way:
printf %q
for all quoting and escaping. This is a best-practice approach: Using literal quotes means that literals inside the data being generated are able to escape those quotes.&&
through shell quoting will make them into literal data. Thus, we want the&&
to be literal data rather than syntax when we're passing it as an argument to-c
, but not literal data when handled by the remote shell -- this is why it's only present in the last quoting pass.Even though a good suggestion was made in the comments, as a general way to deal with such issues you could write it this way :
For a better way to prepare the command to be passed as argument to
lxterminal
, please see CharlesDuffy's answer.As for the rest of your code, I would probably use the following :
Using
"$@"
as the if condition could yield 0 (true) and execute the body if argument 1 exists but is empty (null string), but a following argument is not. I I assume this is not what you want.I am unsure if "@1" is a typo or if it really is intended to mean a directory named "@1" inside your "Projects/Clients" directory. But the
"$1"
argument tocd
should be quoted, for sure.