Skip to main content
The 2024 Developer Survey results are live! See the results
Asked
Viewed 70 times
0

I have tried the following:

myprogram $'Hello $HOME'

But it did not work (the command line argument received by myprogram was Hello $HOME, and not the value of $HOME).

2
  • myprogram "Hello $HOME"?
    – Cyrus
    Commented Jan 3, 2018 at 14:16
  • @ilkkachu: Good catch.
    – Cyrus
    Commented Jan 3, 2018 at 14:22

2 Answers 2

3

Apart from interpreting the C-style backslash-escapes, $'...' works like a single-quoted string. At least Bash's manual mentions this, right at the end of the page on "ANSI-C quoting":

The expanded result is single-quoted, as if the dollar sign had not been present.

So, no. That's probably why they chose to use single-quotes for that (or the other way around), though I don't know about the history of the feature.

You'll have to use double-quotes, and change the quotes as necessary. i.e.

$ myprogram "Hello $HOME"

or with a tab mixed in

$ myprogram $'Hello\t'"$HOME"
2
  • The feature comes from ksh93. Added to bash in 2.0 (1996), zsh in 3.1.5 (1998) mksh in R39b (2010) Commented Jan 3, 2018 at 14:43
  • @StéphaneChazelas, I would have guessed ksh. I mostly wondered if there was a conscious reason to make it a variant of single-quoting instead of double-quoting. Maybe expanding variables is more useful in strings translated with $"" so double-quotes got used for that? But it doesn't matter enough that I would start looking.
    – ilkkachu
    Commented Jan 3, 2018 at 15:24
0

You have to write it as

$ myprogram "Hello $HOME"

Bash will preserve the literal meaning of anything enclosed within double quotes except $,`(back-tick) or .So Here it considers "Hello" as the value while $Hello preserves its value as a variable.

You must log in to answer this question.

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

...' in bash? - Unix & Linux Stack Exchange">
Skip to main content
The 2024 Developer Survey results are live! See the results
Asked
Viewed 70 times
0

I have tried the following:

myprogram $'Hello $HOME'

But it did not work (the command line argument received by myprogram was Hello $HOME, and not the value of $HOME).

2
  • myprogram "Hello $HOME"?
    – Cyrus
    Commented Jan 3, 2018 at 14:16
  • @ilkkachu: Good catch.
    – Cyrus
    Commented Jan 3, 2018 at 14:22

2 Answers 2

3

Apart from interpreting the C-style backslash-escapes, $'...' works like a single-quoted string. At least Bash's manual mentions this, right at the end of the page on "ANSI-C quoting":

The expanded result is single-quoted, as if the dollar sign had not been present.

So, no. That's probably why they chose to use single-quotes for that (or the other way around), though I don't know about the history of the feature.

You'll have to use double-quotes, and change the quotes as necessary. i.e.

$ myprogram "Hello $HOME"

or with a tab mixed in

$ myprogram $'Hello\t'"$HOME"
2
  • The feature comes from ksh93. Added to bash in 2.0 (1996), zsh in 3.1.5 (1998) mksh in R39b (2010) Commented Jan 3, 2018 at 14:43
  • @StéphaneChazelas, I would have guessed ksh. I mostly wondered if there was a conscious reason to make it a variant of single-quoting instead of double-quoting. Maybe expanding variables is more useful in strings translated with $"" so double-quotes got used for that? But it doesn't matter enough that I would start looking.
    – ilkkachu
    Commented Jan 3, 2018 at 15:24
0

You have to write it as

$ myprogram "Hello $HOME"

Bash will preserve the literal meaning of anything enclosed within double quotes except $,`(back-tick) or .So Here it considers "Hello" as the value while $Hello preserves its value as a variable.

You must log in to answer this question.

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