Automatically escape strings in BASH
Posted: Fri Oct 28, 2011 10:51 pm
[PROBLEM]
If you're dealing with input strings in bash which contain special characters like spaces " ", brackets "(", etc... - it's necessary to escape those characters.
For example, if you'd try to call a script and pass it a string as argument which contains special characters, like this:
[SOLUTION]
Now, $FILENAME will contain the escaped string as follows:
If you're dealing with input strings in bash which contain special characters like spaces " ", brackets "(", etc... - it's necessary to escape those characters.
For example, if you'd try to call a script and pass it a string as argument which contains special characters, like this:
it won't be enough to read it like this:./myscript "my test file (1).txt"
Code: Select all
FILENAME="$1"
[SOLUTION]
Code: Select all
FILENAME=$(printf '%q' "$1")
my\ test\ file\ \(1\).txt