# create a file name containing a backslash character \ file=${HOME}/Desktop/'te:st\file'.txt echo "${file}" echo "${file}" | sed -n -e 'l' echo 'This is a test case for a file name containing a backslash \ character!' > "${file}" open -e "${file}" set -vx # note: avoid trailing spaces in ed commands cat <<EOF | /bin/ed -s "${file}" H ,g|^This|s|test case|SUCCESSFUL TEST CASE| w EOF open -e "${file}" # escape backslashes cat <<EOF | /bin/ed -s "${file//\\/\\\\}" H ,g|^This|s|test case|SUCCESSFUL TEST CASE| w EOF open -e "${file}" # printf "%q" help printf | sed -E "s/(%q)/$(printf '\e[1m\\1\e[m')/" echo "${file}" echo "$(printf "%q" "${file}")" # cf. help printf echo "$(printf "%q" "${file}")" | sed -n -e 'l' # escape file name cat <<EOF | /bin/ed -s "$(printf "%q" "${file}")" H ,g|^This|s|backslash|BACKSLASH| w EOF open -e "${file}" echo "${file}" echo "${file}" | sed -n -e 'l' file="${file//\\/\\\\}" echo "${file}" echo "${file}" | sed -n -e 'l' # references man bash 2>/dev/null | less -p 'backslash' man bash 2>/dev/null | less -p 'Each command in a pipeline' man bash 2>/dev/null | less -p 'Functions are executed' help printf | sed -E "s/(%q)/$(printf '\e[1m\\1\e[m')/" open http://en.wikipedia.org/wiki/Filename # "Each command in a pipeline is executed as a separate process (i.e., in a subshell)." # From: man bash # "Functions are executed in the context of the current shell; no new process is created to # interpret them (contrast this with the execution of a shell script)." # From: man bash # "Unix-like systems are an exception, as the only control character forbidden in file names # is the null character, as that's the end-of-string indicator in C. Trivially, Unix also # excludes the path separator / from appearing in filenames." # From: http://en.wikipedia.org/wiki/Filename
Never been to CodeSnippets before?
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Debugging a file name with a backslash character in Bash (See related posts)
You need to create an account or log in to post comments to this site.
Related Posts
» mycmds in awk shell osx mac bash bashrc unix debug profile escape alias
» snippet in shell osx mac html bash unix commandline web download convert textutil ed snippet
» Terminal window commands in shell osx mac bash unix osascript text window terminal color title bar escape sequence background
» Terminal window commands in shell osx mac bash unix code osascript text window terminal color title bar escape background ansi
» ANSI terminal color chart in shell osx mac bash unix code color escape ansi chart
» kv - launch Keyboard Viewer in shell osx mac bash unix keyboard launch viewer character
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails