Skip to main content
Tweeted twitter.com/StackUnix/status/1083876459628937217
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k
Source Link
millimoose
  • 283
  • 1
  • 5

How to have a script work with "$@" or a default list of parameters while not breaking paths with whitespace?

I want a script that will run another utility over some default paths if no parameters are passed to it; ideally I want this safe for paths that contain spaces.

So far I have script.sh:

#!/bin/sh
base=$(dirname "$0")
exec touch "${@:-"$base/aaa" "$base/bbb"}"

If I put this into a folder called "foo bar" and run it as:

foo\ bar/script.sh

I want it to should end up doing:

touch foo\ bar/aaa foo\ bar/bbb

i.e. create files "aaa" and "bbb" under "foo bar", the directory in which the script is located.

Instead I get the error

touch: cannot touch 'foo bar/aaa foo bar/bbb': No such file or directory

(If I pass in parameters to the script it seems to work fine. Presumably removing the outer quotes in the last command would reverse my cases.)