3

I have a sh script for initializing dzen2, and I want to use Source Code Pro font to use there. Here's the code...

param="-p -dock -fn 'Source Code Pro-10'"

# the generated_output function is not important
generated_output | dzen2 $param

The problem is, the script doesn't work! dzen2 doesn't started... After some search I found that the problem may occur because of some problem with single quote ' or double quote " that I don't really understand...

I have tried using this:

param=$'-p -dock -fn \'Source Code Pro-10\''
# or something like this
param='-p -dock -fn '"'"'Source Code Pro-10'"'"

Still doesn't work...
What's wrong with this script?

1

1 Answer 1

6

If you want to keep track of several separate strings (arguments) and later use them as separate strings, do not store them in the same string. Use an array instead:

param=(-p -dock -fn 'Source Code Pro-10')
generated_output | dzen2 "${param[@]}"

The quoted expansion "${param[@]}" will expand to the list of strings in the array param. Note that this means that Source Code Pro-10 will be a single argument, which is what you want. If you forget the quotes around the expansion, the shell will continue to split each string on $IFS characters (space, tab, and newline by default) and then apply filename globbing to each generated word.

Your code relies on the shell to split the string -p -dock -fn 'Source Code Pro-10' into arguments for your command. Since the shell splits on space, tab, and newline by default, this would result in the substring 'Source Code Pro-10' turning into the separate arguments 'Source, Code, and Pro-10' (note that the single quotes are also included in the string).

Related:


In a sh script, you would instead use the list of positional parameters:

set -- -p -dock -fn 'Source Code Pro-10'
generated_output | dzen2 "$@"

This would also work in the bash shell.

0

Not the answer you're looking for? Browse other questions tagged .