0

Here is the line.

    variable=`ls -A $1 | grep '[abc]'; ls -1 $1`

I understand the line before the semi-colon but I do not understand how this line works after semi-colon. Does it

  after the command(variable assignment) 
  and then run the command after semi-colon?

Then the following line would be same?

    variable=`ls -A $1 | grep '[abc]' | ls -1 $1`

Thanks,

Elliptical view
  • 3,921
  • 4
  • 27
  • 46

2 Answers2

2

the semicolon simply separates two consecutive commands. the manpage (man bash) says (in the chapter Lists):

Commands separated by a ; are executed sequentially

e.g. the following fill first run the command foo and then the command bar.

foo; bar

to parse your example correctly, you first have to build a hierarchical model of it:

 variable=`ls -A $1 | grep '[abc]'; ls -1 $1`

can be broken down to variable=... (something within back-ticks) and ls -A $1 | grep '[abc]'; ls -1 $1 (the thing within backticks).

backticks are used get the output of a command (see the Command substitution in man bash); so you are assigning the output of the entire ls -A $1 | grep '[abc]'; ls -1 $1 to the variable.

the output of the substutited command is the list of files matching a certain pattern (including hidden hildes) appended with the full list of files (whatever that can be used for...)

umläute
  • 6,472
1

No, they aren't the same, the latter would pipe grep into ls (which is almost certainly not what you want). The semicolon simply separates two different commands.

Firstly, you probably want to start using the newer, more flexible $( ... ) instead of the backtick.

Take this as an example:

$ var=`echo foo; echo bar`
$ echo "$var"
foo
bar

You can think of the backticks (or $( ... )) as returning whatever was printed on standard output during the execution of the code contained between them.

In your case, it looks at the output of ls -A $1 once, and looks for a line in its output containing a, b, or c, and then prints a single column output of ls -1 $1. In general this is a bad idea, you shouldn't parse ls.

AdminBee
  • 22,803
Chris Down
  • 125,559
  • 25
  • 270
  • 266