Home > OS >  Create file from variable value with spaces
Create file from variable value with spaces

Time:07-11

I would like to create a file from variable value with spaces.

  • Variable Value: Los Angeles
  • Create Filename: "Los Angeles.txt"

Script Snippet:

ProjectSite=" Los Angeles"
fname=`echo ${ProjectSite} | sed 's,^ *,,; s, *$,,'`".txt"
touch "${fname}"

Current Output:

'Los Angeles.txt'

File is created along with single quote.

Required Output:

Los Angeles.txt

CodePudding user response:

The file name is correct as is. It doesn't have single quotes; that's simply how ls displays it. You can use ls -N to tell ls not to quote file names with special characters:

❯ touch 'Los Angeles.txt'
❯ ls
'Los Angeles.txt'
❯ ls -N
Los Angeles.txt
  • Related