LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-07-2016, 11:27 AM   #1
ghborrmann
Member
 
Registered: May 2004
Location: Louisville KY
Distribution: Fedora 25
Posts: 32

Rep: Reputation: 0
Bash: Problem with Escaping the Space Character


In a bash script, I have the command:
Code:
ls -l --time-style=+'%y/%m/%d %H:%M:%S'
which works perfectly. I have tried to put all the options into a bash variable, but can't get ls to work that way:
Code:
options="-l --time-style=+'%y/%m/%d %H:%M:%S'"
ls $options
ls: cannot access '%H:%M:%S': No such file or directory
I have tried escaping the space character, with no effect. How can I get this to work?
 
Old 07-07-2016, 12:21 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Try this:

Code:
options=(-l --time-style=+'%y/%m/%d %H:%M:%S')
ls "${options[@]}"
Don't ask why it works - I don't know. I was just intrigued by your question, found the following thread, and played around a little with the arrays option:

http://unix.stackexchange.com/questi...ial-characters

Hopefully, someone else will elucidate...
 
2 members found this post helpful.
Old 07-07-2016, 12:37 PM   #3
ghborrmann
Member
 
Registered: May 2004
Location: Louisville KY
Distribution: Fedora 25
Posts: 32

Original Poster
Rep: Reputation: 0
Works like a charm! Thanks!
 
Old 07-07-2016, 12:39 PM   #4
john2
Member
 
Registered: Aug 2004
Location: Wales
Distribution: Manjaro, Kubuntu, MX
Posts: 102

Rep: Reputation: 29
I was intrigued too. The only way I could find is to use eval:
Code:
options="-l --time-style=+'%y/%m/%d %H:%M:%S'"
eval "ls" $options
hydrurga's way looks neater though . It probably works by avoiding double quotes - backslash within double quotes only works as escape character when followed by ‘$’, ‘`’, ‘"’, ‘\’, or newline. See here:https://www.gnu.org/software/bash/ma...le-Quotes.html

Edit: The quotes around ls are optional.

Last edited by john2; 07-08-2016 at 02:43 PM. Reason: Extra information
 
1 members found this post helpful.
Old 07-07-2016, 12:40 PM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by ghborrmann View Post
Works like a charm! Thanks!
Nice. That page I linked to makes very interesting reading too.

Can you mark this thread as "Solved" please? Cheers.
 
Old 07-07-2016, 05:58 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Another way is to alias ls, like
Code:
alias lstime='ls -l --time-style=+"%y/%m/%d %H:%M:%S"'
 
2 members found this post helpful.
  


Reply

Tags
bash, escape



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing text which contains html tags - character escaping problem gordium Linux - Newbie 10 11-03-2015 06:03 PM
[SOLVED] Bash regex to remove files with 3 character followed by a space Kearneyman Linux - General 6 09-15-2011 02:28 PM
Automatic special character escaping in Bash scripts? wipe Linux - Software 1 06-05-2009 06:41 PM
escaping ':' character in bash script paulyche Linux - General 2 11-07-2006 07:18 AM
perl post character escaping kapilcool Programming 3 12-20-2005 07:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration