LinuxQuestions.org
Visit Jeremy's Blog.
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, 12:27 PM   #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, 01:21 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924
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, 01: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, 01:39 PM   #4
john2
Member
 
Registered: Aug 2004
Location: Wales
Distribution: Kubuntu, Mageia, OpenSUSE
Posts: 100

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 03:43 PM. Reason: Extra information
 
1 members found this post helpful.
Old 07-07-2016, 01:40 PM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924
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, 06:58 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:03 PM
[SOLVED] Bash regex to remove files with 3 character followed by a space Kearneyman Linux - General 6 09-15-2011 03:28 PM
Automatic special character escaping in Bash scripts? wipe Linux - Software 1 06-05-2009 07:41 PM
escaping ':' character in bash script paulyche Linux - General 2 11-07-2006 08:18 AM
perl post character escaping kapilcool Programming 3 12-20-2005 08:42 AM

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

All times are GMT -5. The time now is 10:27 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