LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2020, 03:35 AM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
To enclose a string with quote pair (or its double) in a bigger string


How to enclose a string with quote pair, or its double, in a bigger string from the Bash expansion ?

Code:
u=`echo fo* b* fo*b`
echo $u

$ foo bar foobar foo bar
the last is supposed to be a space contained string enclosed with quote pair, or its double, so

$ foo bar foobar 'foo bar'
i.e
Quote:
u="foo bar foobar 'foo bar'"
Thanks before
 
Old 12-22-2020, 04:07 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
There are several problems:
1. do not use backick and echo
Code:
#wrong
u=`echo fo* b* fo*b`
#good
u="fo* b* fo*b"
# or probably something else, but it depends on the globbing [if you want].
2. echo $u is misleading
Code:
u="sdff  asd                 dsaf"
echo $u
sdff asd dsaf
echo "$u"
sdff  asd                 dsaf
the variable without " will not be protected and bash will evaluate the line before executing that echo (and whitespace will be collapsed).

finally I think you need arrays, something like this:
Code:
u=( fo* b* fo*b )
echo ${u[0]}
foo
echo ${u[3]}
foo bar
relying on the bash command line interpreter is not a good idea anyway. But anyway, we need to know more to give better help...
 
Old 12-22-2020, 04:33 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Another possibility
Code:
$ tree
── a b
    ├── a`b'c
    ├── a blank -> a space
    ├── a,comma
    ├── a\012newline
    ├── a|pipe
    ├── a space
    ├── dah -> examples
    ├── d e
    ├── `def'
    ├── dir\012newline
    ├── examples
    │   ├── f1
    │   ├── f2
    │   ├── top-1m.csv
    │   └── top-1m.csv.zip
    ├── g"h
    └── --help

6 directories, 12 files
$ printf -v x "%q\n" a*/a*;echo "$x"
a\ b/a\`b\'c
a\ b/a\ blank
a\ b/a\,comma
$'a b/a\nnewline'
a\ b/a\|pipe
a\ b/a\ space

Last edited by shruggy; 12-22-2020 at 04:47 AM.
 
1 members found this post helpful.
Old 12-22-2020, 04:52 AM   #4
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by shruggy View Post
Another possibility
Code:
$ tree
── a b
    ├── a`b'c
    ├── a blank -> a space
    ├── a,comma
    ├── a\012newline
    ├── a|pipe
    ├── a space
    ├── dah -> examples
    ├── d e
    ├── `def'
    ├── dir\012newline
    ├── examples
    │   ├── f1
    │   ├── f2
    │   ├── top-1m.csv
    │   └── top-1m.csv.zip
    ├── g"h
    └── --help

6 directories, 12 files
$ printf -v x "%q\n" a*/a*;echo "$x"
a\ b/a\`b\'c
a\ b/a\ blank
a\ b/a\,comma
$'a b/a\nnewline'
a\ b/a\|pipe
a\ b/a\ space
Thanks for the excellence!
 
Old 12-23-2020, 05:06 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Get used to newline separators:
Code:
u=$(printf "%s\n" fo* b* fo*b)
Now
Code:
echo "$u"
will show the filenames with embedded space characters.

If you want to field-split on newlines only, consider setting IFS
Code:
oIFS=$IFS
IFS="
"
# split on IFS into a word list
for word in $u
do
  echo "$word"
done
# split on IFS into an array
array=($u)
printf "%s\n" "${array[@]}"
# restore IFS
IFS=$oIFS
Note that the zsh does not field-split a $var, needs $=var
 
  


Reply

Tags
bash, quoting, shell


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
'find' space-contained search result is printed with enclosing '' (a quote pair) BudiKusasi Programming 3 10-28-2020 12:33 AM
add tags to enclose a string bishop2001 Linux - Newbie 6 05-23-2015 07:30 AM
[SOLVED] How to pass a single quote inside of a single quote... trist007 Linux - Newbie 2 02-09-2011 07:07 PM
sed : Convert double quote to singe quote pgb_710 Programming 6 01-21-2010 07:59 AM
when to quote or not to quote variable names - mktemp problem glinux Linux - Newbie 3 02-20-2009 04:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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