LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2015, 01:30 AM   #1
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Rep: Reputation: Disabled
brace expansion + new line


Hi all,

How do i make

Code:
[alan@racnode1 scripts]$ echo alan{1..3}
alan1 alan2 alan3
to

Quote:
alan1
alan2
alan3
instead ?


Tried -e + /n to no avail.
Code:
[alan@racnode1 scripts]$ echo alan{1..3}\n
alan1n alan2n alan3n
[alan@racnode1 scripts]$ echo -e alan{1..3}\n
alan1n alan2n alan3n
[alan@racnode1 scripts]$ echo -e "alan{1..3}\n"
alan{1..3}

[alan@racnode1 scripts]$
Thanks.

Regards,
Noob
 
Old 01-09-2015, 01:56 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Messy but:

Code:
echo alan{1..3}|sed 's/ /\n/g'
Messier:

Code:
for x in (1..3); do echo alan$x;done
Although I'm sure some shell guru will come along and explain some strange switch that can be used with the shell
 
Old 01-09-2015, 02:32 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,803

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
more or less:
echo -e alan{1..3}"\013\015"
why do you need that?
 
Old 01-11-2015, 10:50 AM   #4
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by TenTenths View Post
Messier:
for x in (1..3); do echo alan$x;done
Doesn't works in bash shell.
 
Old 01-11-2015, 01:53 PM   #5
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
more or less:
echo -e alan{1..3}"\013\015"
why do you need that?
hi pan64, nothing much, testing brace expansion while reading "Bash Guide For Beginner" here and there and encounter this phrase below

Quote:
3.3.4. Double quotes
Using double quotes the literal value of all characters enclosed is preserved, except for the dollar sign, the
backticks (backward single quotes, ``) and the backslash.
The dollar sign and the backticks retain their special meaning within the double quotes.
The backslash retains its meaning only when followed by dollar, backtick, double quote, backslash or
newline. Within double quotes, the backslashes are removed from the input stream when followed by one of
these characters. Backslashes preceding characters that don't have a special meaning are left unmodified for
processing by the shell interpreter.
I am trying to simulate how newline works with backslash in double quotes but cant understand how to simulate a newline.

Code:
[root@racnode1 ~]# echo "ok now \\ is working as required, showing the \\"
ok now \ is working as required, showing the \

[root@racnode1 ~]# echo "ok now \\ is working as required, showing \$(date) as \$(date)"
ok now \ is working as required, showing $(date) as $(date)
[root@racnode1 ~]# 

[root@racnode1 ~]# echo "ok now \\ is working as required, showing newline as \\n instead"
ok now \ is working as required, showing newline as \n instead
All is okay until i add a "-e"

Code:
[root@racnode1 ~]# echo -e "ok now \\ is working as required, showing newline as \\n instead"
ok now \ is working as required, showing newline as 
 instead

Why isn't "\" working as it suppose to be when i add a "-e" , the 1st "\" is suppose to remove any special meaning from the subsequent character which is "\n" ...

Regards,
Noob
 
Old 01-11-2015, 08:13 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
try the following instead:
Code:
$ printf "%s\n" alan{1..3}
alan1
alan2
alan3
 
1 members found this post helpful.
Old 01-12-2015, 02:05 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,803

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
\013 and \015 are octal chars and evaluated for me, but the solution of jpollard looks much better.

So regarding evaluation: it is really hard, or better to say complex. First always the current shell will try to interpret the string you entered. Next, the command will try to use (and do what it want) the command line parameters passed by the shell. In your case the question is: who will evaluate the \ and { } ?
echo -e is able to do interpret \<something> (see man page of bash again), but { } are always evaluated by the shell. Using 'some string' will disable the shell evaluation, but using "some string" will enable that, therefore the shell will also try to "understand" those \ chars (that was what you posted).
 
Old 01-12-2015, 02:29 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by veerain View Post
Doesn't works in bash shell.
My mistake, () should be {}
 
Old 01-12-2015, 05:30 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
There is no need for the \013, as that is a carriage return. Putting it there is a DOS thing, and can cause problems depending on how the output is used - for instance, adding a line to a configuration file can cause it to be an illegal character. And that can be rather difficult to see, and the error may not say "illegal character" either (bad format, missing ":", ... or other errors).
 
Old 01-12-2015, 08:12 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,803

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
this is how it was running on ubuntu and on suse:
Code:
user@host:~$ echo -e alan{1..3}"\013\015"
alan1
 alan2
 alan3

user@host:~$ echo -e alan{1..3}"\015"
 alan3
user@host:~$ echo -e alan{1..3}"\013"
alan1
      alan2
            alan3
 
Old 01-12-2015, 04:03 PM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
013 is a vertical tab... 012 is a newline.

Simpler to remember "\n".
 
  


Reply



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
[SOLVED] Bash - brace expansion using variable student04 Linux - Software 3 03-07-2017 05:04 AM
bash brace expansion won't output two zero hahacc Linux - Newbie 6 03-31-2014 03:49 AM
Brace expansion: does this kind exist? exscape Linux - Software 4 04-28-2009 03:45 AM
Avoiding Shell Script Brace Expansion Woodsman Slackware 4 05-31-2008 09:36 AM
bash: extra stuff in brace expansion jhwilliams Programming 4 09-07-2007 02:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:49 AM.

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