Skip to content

Instantly share code, notes, and snippets.

@rk295
Created October 10, 2014 12:25
Show Gist options
  • Save rk295/cd4244346a8c858c8318 to your computer and use it in GitHub Desktop.
Save rk295/cd4244346a8c858c8318 to your computer and use it in GitHub Desktop.
bash variable foo
$ export foo=bar
$ export "baz"$foo=wibble
$ echo $bazbar
wibble
@fidian
Copy link

fidian commented Jan 21, 2015

If you are potentially assigning quotes, special shell characters and spaces, I would suggest a slightly different approach.

export foo=bar
printf -v "baz$foo" '%s' "wibble"
echo $bazbar

Works better for odd things like the following.

printf -v "test" '%s' "'single' and"'"double" quotes, '"\nnewlines, backslashes\\ and much more"'!'
echo -e "$test"

@fidian
Copy link

fidian commented Jan 21, 2015

Sorry, also forgot about this:

foo=bar
bar=baz
echo $foo    # bar
echo ${!foo}    # baz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment