Bash file name with spaces

Source: Internet
Author: User
Tags macbook

http://www.keakon.net/2011/10/20/bash%E4%B8%8B%E5%A4%84%E7%90%86%E5%8C%85%E5%90%AB%E7%A9%BA%E6%A0%BC%E7%9A%84% e6%96%87%e4%bb%b6%e5%90%8d

For example, I create 3 files with a space in the current folder:

keakons-MacBook-Pro:test keakon$ touch "test 1"keakons-MacBook-Pro:test keakon$ touch "test 2"keakons-MacBook-Pro:test keakon$ touch "test 3"keakons-MacBook-Pro:test keakon$ lstest 1test 2test 3


Then the For loop outputs the file name:

keakons-MacBook-Pro:test keakon$ for file in `ls`;> do echo $file;> donetest1test2test3

As you can see, the file names are separated.

Replication does not work either:

keakons-macbook-Pro:test keakon$ mkdir. /bakkeakons-macbook-Pro:test keakon$For fileInch' ls ';Do CP"$file". /bak; DoneCp:bak is a directory (not copied). CP: test:no such file or directory< Span class= "Hljs-symbol" >CP: 1: No such file or directoryCP: test:no such file or directoryCP: 2: No Such file or directoryCP: test:no Such file or directoryCP: 3: No such file or directory       


To solve this problem, of course, start with the word delimiter. In bash, $ifs (Internal Field Separator) is used as a variable with the content "\n\t":

keakons-MacBook-Pro:test keakon$ echo $IFSkeakons-MacBook-Pro:test keakon$ echo "$IFS" | od -t x10000000 20 09 0a 0a 0000004keakons-MacBook-Pro:test keakon$ echo "" | od -t x10000000 0a 0000001


Then change it to "\n\b" and remember to save it before modifying it:

keakons-MacBook-Pro:test keakon$ SAVEIFS=$IFSkeakons-MacBook-Pro:test keakon$ IFS=$(echo -en "\n\b")


It's OK to execute the above command now:

 keakons-macbook-pro:test keakon$ for file in ' ls '; do echo  $file; donetest 1test 2 test 3keakons-macbook-pro:test keakon$ for file in ' ls '; do cp  " $file". /bak; donekeakons-macbook-pro:test keakon$ ls. /baktest 1test 2test 3   


Finally, don't forget to restore the $ifs:

keakons-MacBook-Pro:test keakon$ IFS=$SAVEIFSkeakons-MacBook-Pro:test keakon$ echo "$IFS" | od -t x10000000 20 09 0a 0a 0000004keakons-MacBook-Pro:test keakon$ IFS=$(echo -en " \n\t")keakons-MacBook-Pro:test keakon$ echo "$IFS" | od -t x10000000 20 0a 09 0a 0000004

Bash file name with spaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.