Created
October 13, 2015 18:05
-
-
Save dirk-thomas/b7c9c77a996518c2fe5e to your computer and use it in GitHub Desktop.
Bash array quoting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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