LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   brace expansion + new line (https://www.linuxquestions.org/questions/linux-newbie-8/brace-expansion-new-line-4175530504/)

szejiekoh 01-09-2015 01:30 AM

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

TenTenths 01-09-2015 01:56 AM

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 :)

pan64 01-09-2015 02:32 AM

more or less:
echo -e alan{1..3}"\013\015"
why do you need that?

veerain 01-11-2015 10:50 AM

Quote:

Originally Posted by TenTenths (Post 5298081)
Messier:
for x in (1..3); do echo alan$x;done

Doesn't works in bash shell.

szejiekoh 01-11-2015 01:53 PM

Quote:

Originally Posted by pan64 (Post 5298092)
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

jpollard 01-11-2015 08:13 PM

try the following instead:
Code:

$ printf "%s\n" alan{1..3}
alan1
alan2
alan3


pan64 01-12-2015 02:05 AM

\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).

TenTenths 01-12-2015 02:29 AM

Quote:

Originally Posted by veerain (Post 5299171)
Doesn't works in bash shell.

My mistake, () should be {}

jpollard 01-12-2015 05:30 AM

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).

pan64 01-12-2015 08:12 AM

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


jpollard 01-12-2015 04:03 PM

013 is a vertical tab... 012 is a newline.

Simpler to remember "\n".


All times are GMT -5. The time now is 04:46 PM.