Trying to keep spaces in the variable for read with the \.  $ echo "Drag folder to be copied to terminal and press ENTER." $ read FILE /Users/UserName/Desktop/Folder\ With\ Spaces\ In\ it $ echo "$FILE" /Users/UserName/Desktop/Folder With Spaces In it   The \ have been removed. I need them to remain as it will be used later in copy. Example.  $ cp -R $FILE /Volumes/USBName    I have played with different quotation marks and looked around but nothing has worked for me so far. I may have to change the IFS, not sure?
1847
Or, an introductory guide to robust filename handling and other string passing in shell scripts.  I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).  I encountered a problem such as the following:   I have a file name containing a space hello world, and it was treated as two separate files hello and world. I have an input line with two consecutive spaces and they shrank to one in the input. Leading and trailing whitespace disappears from input lines. Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is is actually the name of files. There is an apostrophe ' (or a double quote ") in the input and things got weird after that point. There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators).   What is going on and how do I fix it?
9258
I've got a script that scp's a file from remote host back to local. Sometimes the file names contain spaces. scp does not like spaces in its file names. For some reason my attempts at handling the spaces have not resulted in the correct scp path.  Code:  PATH=/var/root/Documents/MyFile OG-v1.2.3.pkg scp $PATH Me@10.1.10.33:/Users/Me/Desktop   Results in  Cannot find directory: var/root/Documents/MyFile Cannot find directory: OG-v1.2.3.pkg   Enclosing PATH in quotes "$PATH" gives the same error.  Swapping the spaces for escaped spaces also is not working, although as far as I can tell it should:  ESC_PATH=${PATH/' '/'\ '}   although printing the escaped path shows that the edit worked:  echo $ESC_PATH &gt; /var/root/Documents/MyFile\ OG-v1.2.3.pkg
107624
To push a file from a Linux terminal to a Windows system, the following two examples work just fine.  scp /home/user.name/file.html user.name@local.ip.num:/C:/Users/user.name/test_folder/file.html  scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test_folder/file.html"   I need to do this where the local folder has spaces and I cannot change the name,  say /C:/Users/user.name/test folder/  All of the following fail with the message scp: ambiguous target  scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test folder/file.html"  scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test\ folder/file.html"  scp /home/user.name/file.html user.name@local.ip.num:"'/C:/Users/user.name/test\ folder/file.html'"  scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test\\ folder/file.html"  scp /home/user.name/file.html user.name@local.ip.num:"'/C:/Users/user.name/test\\ folder/file.html'"  scp /home/user.name/file.html user.name@local.ip.num:"/C:/Users/user.name/test\\\ folder/file.html"  scp /home/user.name/file.html user.name@local.ip.num:"'/C:/Users/user.name/test\\\ folder/file.html'"   How do I get this to work?
23932
I'm on a Mac and the cp and mv commands are killing me due to them copying the contents of a directory if a trailing slash is present. It has bitten me more than once when I use tab completion to specify the directory I want to copy.  I have noticed zsh has a neat feature that removes the trailing slash if it's not needed after a space or enter key is pressed. Is there any way to have bash mimic this behavior?
11493
From      Storing the regular expression in a shell variable is often a useful way to avoid problems with quoting characters that are special to the   shell. It is sometimes difficult to specify a regular expression   literally without using quotes, or to keep track of the quoting used   by regular expressions while paying attention to the shell’s quote   removal. Using a shell variable to store the pattern decreases these   problems. For example, the following are equivalent:  pattern='[[:space:]]*(a)?b' [[ $line =~ $pattern ]]       and  [[ $line =~ [[:space:]]*(a)?b ]]       If you want to match a character that’s special to the regular   expression grammar, it has to be quoted to remove its special meaning.   This means that in the pattern xxx.txt, the . matches any   character in the string (its usual regular expression meaning), but in   the pattern "xxx.txt" it can only match a literal .. Shell   programmers should take special care with backslashes, since   back-slashes are used both by the shell and regular expressions to   remove the special meaning from the following character. The following   two sets of commands are not equivalent:  pattern='\.'  [[ . =~ $pattern ]] [[ . =~ \. ]]  [[ . =~ "$pattern" ]] [[ . =~ '\.' ]]       The first two matches will succeed, but the second two will not,   because in the second two the backslash will be part of the pattern to   be matched. In the first two examples, the backslash removes the   special meaning from ., so the literal . matches. If the string in   the first examples were anything other than ., say a, the pattern   would not match, because the quoted . in the pattern loses its   special meaning of matching any single character.   How is storing the regular expression in a shell variable  a useful way to avoid problems with quoting characters that are special to the shell?  The given examples don't seem to explain that. In the given examples, the regex literals in one method and the values of the shell variable pattern in the other method are the same.   Thanks.
54303
If you take this code:  echo -e '\t\t\tString' | grep '^[\t]*String'   the result is blank because it doesn't match, yet this:  echo -e '\t\t\tString' | grep $'^[\t]*String'   works. I swear that I must have used the first line's code a hundred times in my scripts and in the terminal, without ever using the "$" character like that, and it's always seemed to work. Has there been some recent change? Why does it need the "$" character? Or am I doing something wrong?
95760
Just noticed it works on Maverick.. the colon character : is valid for file and directory names (I accidentally entered it for a filename in Atom).  Can't find any reference to it becoming valid though ( still marks it as invalid).  It works in the Atom editor.  I can create a file called : with nano.  This works echo 'test' &gt; : &amp;&amp; cat :  In TextWrangler it only works if the file already exists but won't create it (with command line tools installed edit :)  Is this new from Mavericks? Works in Yosemite? Before?  Edit: A few more tests:    TextEdit won't allow me to enter it and always replace it with - Finder won't allow me to create a folder with the name :  A : file created via command line is displayed as / in Finder.. and double clicking it will open it in TextEdit. TextEdit will load its content but show the name / in the title bar.
19937
I'm working on some scripting:  for x in `find ./ -name *.pdf` do   echo pathname $x done   My filenames are Test1 ( Volume II), Test2 ( Volume II). I'm getting a return of  pathname Test1 pathname ( pathname Volume pathname II …   How do I get it to stay as one variable?
31032
I'm paranoid about this, but hopefully it's a simple question:  Is there any difference between  cat file1 | egrep -oP "[Mm]y string" &gt; /home/user/file.txt   and  cat file1|egrep -oP "[Mm]y string"&gt;/home/user/file.txt   i.e., does it matter if I have spaces padding the pipe (|) and redirection (>) characters? I'm really paranoid that having spaces there the spaces will somehow end up in the output file when I don't want that. Can anyone clarify? (Using Ubuntu 12/bash if that matters.)
108847
If I type ' (single quote, next to the &lt;enter&gt; button) in the terminal and then press enter it drops to a &gt; prompt.  What exactly is this, what's it used for, and what type of commands can I enter here?    I can &lt;Ctrl-C&gt; out of it but that's all I got.
18366
In bash one can escape arguments that contain whitespace.  foo "a string"   This also works for arguments to a command or function:  bar() {     foo "$@" }  bar "a string"   So far so good, but what if I want to manipulate the arguments before calling foo?  This does not work:  bar() {     for arg in "$@"     do         args="$args \"prefix $arg\""     done      # Everything looks good ...     echo $args      # ... but it isn't.     foo $args      # foo "$args" would just be silly }  bar a b c   So how do you build argument lists when the arguments contain whitespace?
96290
How can I replace spaces with new lines on an input like:  /path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5 etc...  To obtain the following:  /path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5   Note  I'm posting this question to help other users, it was not easy to find a useful answer on UNIX SE until I started to type this question. After that I found the following:  Related question
63267
In  12, using the  shell, I noticed this difference when looking at $path (lowercase) versus $PATH (uppercase).  echo $path      /sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin /usr/home/freebsd/bin   echo $PATH      /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/home/freebsd/bin   One output is delimited by SPACE character, the other by COLON character.   ➥ Why the difference?   Are these two different, separate variables? Or does the lowercase/uppercase trigger some kind of trick or meaning I do not know about?   Is this a zsh feature? Or a feature of FreeBSD?
64994
file contains just 1 line:  aaa   when I run "cat file" it mixes to username  user /dir : cat file aaauser /dir :   What could be the problem ?  Extra Info:  perhaps this is not properly set in bashrc ? ?  PS1='\u \W : '   UPDATE:  shouldnt there be a solution other than including a newline to the file ?  Why would there be no care for the elements that can distort the command line prompt ? ( by not having a new line )
113673
I'm looking at a batch file which defines the following variables:  set _SCRIPT_DRIVE=%~d0 set _SCRIPT_PATH=%~p0    What do %~d0 or %~p0 actually mean? Is there a set of well-known values for things like current directory, drive, parameters to a script? Are there any other similar shortcuts I could use?
70608
I have a variable for color. I use it to set color to strings, by evaluating it inside the string. However, I need to include space after the name(so that the name doesn't contain part of the text). This sometimes looks bad.  How can I avoid using(printing) this space?  Example(Let's say that Red=1 and NC=2):  echo -e "$Red Note: blabla$NC".   Output:  1 Note: blabla2.   Expected output:  1Note: blabla2.
59568
I am looking for a command line or bash script that would add space 5 times before the beginning of each line in a file.  for example:  abc   after adding spaces 5 times        abc