38

I am new to shell script. Can someone help me with command to escape the space with "\ ". I have a variable FILE_PATH=/path/to my/text file ,
I want to escape the spaces alone FILE_PATH=/path/to\ my/text\ file

I tried with tr -s command but it doesnt help

FILE_PATH=echo FILE_PATH | tr -s " " "\\ "

Can somebody suggest the right command !!

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Lolly
  • 34,250
  • 42
  • 115
  • 150

7 Answers7

63

If you are using bash, you can use its builtin printf's %q formatter (type help printf in bash):

FILENAME=$(printf %q "$FILENAME")

This will not only quote space, but also all special characters for shell.

Bao Haojun
  • 966
  • 5
  • 9
13

There's more to making a string safe than just escaping spaces, but you can escape the spaces with:

FILE_PATH=$( echo "$FILE_PATH" | sed 's/ /\\ /g' )
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Would you care to elaborate? I'm guessing that you're suggesting elimination of characters reserved by the shell, e.g., `[ ] ( ) { } ; ' " [:space:] &` as well as non-printable characters. Do you have anything else in mind? – Barton Chittenden Oct 09 '12 at 19:35
  • @Barton It will certainly depend on use, but all of the characters you mention are suspect. Mostly what I have in mind is that attempting to escape characters for any sort of protection is fragile and not something that should be done without great care. – William Pursell Oct 09 '12 at 19:38
9

You can use 'single quotes' to operate on a path that contains spaces:

cp '/path/with spaces/file.txt' '/another/spacey path/dir'

grep foo '/my/super spacey/path with spaces/folder/*'

in a script:

#!/bin/bash

spacey_dir='My Documents'
spacey_file='Hello World.txt'
mkdir '$spacey_dir'
touch '${spacey_dir}/${spacey_file}'
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • 3
    Unless you need to do variable interpolation, I would actually suggest using single quotes rather than double. There are some sneaky characters such as `!` which bash will try to interpolate inside double quotes, but not single. Of course, you are free to cast a pox on anyone who puts bang characters in file names. – Barton Chittenden Oct 11 '12 at 13:52
2

You can do it with sed :

NEW_FILE_PATH="$(echo $FILE_PATH | sed 's/ /\\\ /g')"
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    `FILE_PATH=$(echo $FILE_PATH | sed "s/ /\\\ /g")` You need three backslashes because you have to escape the backslash character both in the shell and in sed. – sevcsik Aug 03 '17 at 18:43
2

Use quotes to preserve the SPACE character

tr is used only for replacing individual characters 1 for 1. Seems to be that you need sed.

echo $FILE_PATH | sed -e 's/ /\\ /'

seems to do what you want.

sureshvv
  • 4,234
  • 1
  • 26
  • 32
1

Do not forget to use eval when using printf guarding.

Here's a sample from Codegen Script under Build Phases of Xcode (somehow PROJECT_DIR is not guarded of spaces, so build was failing if you have a path with spaces):

PROJECT_DIR_GUARDED=$(printf %q "$PROJECT_DIR")
eval $PROJECT_DIR_GUARDED/scripts/code_gen.sh $PROJECT_DIR_GUARDED
kikiwora
  • 1,724
  • 11
  • 7
-4
FILE_PATH=/path/to my/text file
FILE_PATH=echo FILE_PATH | tr -s " " "\\ "

That second line needs to be

FILE_PATH=echo $FILE_PATH | tr -s " " "\\ "

but I don't think it matters. Once you have reached this stage, you are too late. The variable has been set, and either the spaces are escaped or the variable is incorrect.

FILE_PATH='/path/to my/text file'

hymie
  • 1,982
  • 1
  • 13
  • 18