codeCompiler77 codeCompiler77 - 1 year ago 102
Linux Question

Ubuntu Bash Script executing a command with spaces

I have a bit of an issue and i've tried several ways to fix this but i can't seem to.

So i have two shell scripts. This runs a given command in the background and redirect's output.


if test -t 1; then
exec 1>/dev/null

if test -t 2; then
exec 2>/dev/null

"$@" & This file simply starts the emulator (genymotion) as a background process.

CMD="$BK player --vm-name $DEVICE"

This works fine when i have NO spaces in my directory. However, when i try to do:
GENY_DIR="home/user/Documents/My Script/watchdog/genymotion"

which i have no choice at the moment. I get an error saying that the file or directory cannot be found. I tried to put
in quote but it didn't work.

You can test this by trying to run anything as a background process, doesn't have to be an emulator.

Any advice or feedback would be appreciated. I also tried to do.





BK=$( echo "$BK" | sed 's/ /\\ /g' )

Answer Source

Don't try to store commands in strings. Use arrays instead:

GENY_DIR="$HOME/Documents/My Script/watchdog/genymotion"
CMD=( "$BK" "player" --vm-name "$DEVICE" )

Arrays properly preserve your word boundaries, so that one argument with spaces remains one argument with spaces.

Due to the way word splitting works, adding a literal backslash in front of or quotes around the space will not have a useful effect.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download