Skip to content

Instantly share code, notes, and snippets.

@dirk-thomas
Created October 13, 2015 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirk-thomas/b7c9c77a996518c2fe5e to your computer and use it in GitHub Desktop.
Save dirk-thomas/b7c9c77a996518c2fe5e to your computer and use it in GitHub Desktop.
Bash array quoting
#!/usr/bin/env bash
mkdir "/tmp/test_bash"
mkdir "/tmp/test_bash/foo bar"
mkdir "/tmp/test_bash/bar baz"
mkdir "/tmp/test_bash/foo"
mkdir "/tmp/test_bash/bar"
touch "/tmp/test_bash/foo bar/file"
touch "/tmp/test_bash/bar baz/file"
touch "/tmp/test_bash/foo/file"
touch "/tmp/test_bash/bar/file"
str="/tmp/test_bash/foo bar;/tmp/test_bash/bar baz"
echo "str: '$str'"
IFS=";"
array=($str)
unset IFS
echo "array[0]: '${array[0]}'"
echo "array[1]: '${array[1]}'"
echo "array[@]: '${array[@]}'"
args=`python -c "import sys; print(sys.argv[1:]);" "${array[@]}"`
echo "args: '$args'"
result="`find "${array[@]}" -name file`"
echo "results: '$result'"
rm -fr "/tmp/test_bash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment