Having a ls -l command it makes a list on the command prompt and if we want to manipulate it we pipeline it with awk command, for instance if we want to to show specific files.

Is that possible without using the awk command but instead using a shell script, if so what would be the column variables how would we manipulate that on fly list do you have also any suggestions on educative links or books.

EDIT:for example a ls -l|awk '{print $1}' the $1 is referring to the first column,is there any way to do that with a shell script variable, for instance if we would like to run the ls -l command inside a shell script and print the exact filenames of which we are searching for. How to do this?

  • Please edit your question to add an example of what you are describing. I think you are asking how you can manipulate text in columns through a shell script, is that so?
    – terdon
    Commented Dec 9, 2013 at 14:50
  • 1
    You should not parse the output of ls if you have filenames with spaces and/or newlines there is no way you parse the output.
    – Anthon
    Commented Dec 9, 2013 at 14:50

2 Answers 2


Your question right now is not so clear, but I'll have a stab at one of the things I think you are referring to.

If you want to write a script or program that can be used in a pipeline, it must read from standard input and write to standard output. For example, a bash script that adds line numbers:


while read line; do
    echo $index". "$line
    index=$(($index + 1))

If this is called eg.sh you could use it:

ls -1 | ./eg.sh
cat ./eg.sh | ./eg.sh | grep "^5\."

The last one should print out just:

5. echo $index". "$line

The key to getting the input is the read built-in (here's some details).

  • I 've done some research and I want to ask,in awk command $1,$2 e.t.c. are referring to the file's or the command's columns, which are the equivalent variables in shell script for example in a loop "read number name" it reads the first variable of each line of a file how we read the others "columns" of that file separetely?is there any any way or awk is mandatory? Commented Dec 9, 2013 at 19:27

I am not sure I understand your question, but in the simplest case, where you have space-separated columns of text and no spaces within fields, you could do:

echo "col1 col2 col3 col4" | while read one two three four; do echo "$two"; done

The while read [variable names] shell loop will automatically split its input at whitespace into as many variables as you give.

Not the answer you're looking for? Browse other questions tagged .