-1

How can I make a bash script that uses the ls -l command on a given argument? I have a text document containing my shebang ect... but when I add the ls -l command, it only works on my cwd. Say my script is scr, when I run ./scr it will not allow me to enter an argument and simply executes ls -l on my cwd. I need to be able to give the ls -l command a directory as the argument.

0

If you put ls -l "$@" in your script, it will give any arguments given to the bash script to the ls -l command:

#!/bin/bash
ls -l "$@"

Use:

Chris@Chris-PC:~/test$ ./scr.sh
total 4
-rwxrwxr-x+ 1 Me None 21 Feb 15 22:02 scr.sh
Chris@Chris-PC:~/test$ ./scr.sh /
total 489
drwxr-xr-x+  1 Chris None      0 Feb  7 17:09 bin
drwxr-xr-x+  1 Chris None      0 Oct  1  2014 dev
drwxr-xr-x+  1 Chris None      0 Feb  7 17:09 etc
drwxrwxrwt+  1 Chris None      0 Apr 13  2015 home
drwxr-xr-x+  1 Chris None      0 Feb  7 17:09 lib
dr-xr-xr-x  10 Chris None      0 Feb 15 22:03 proc
drwxr-xr-x+  1 Chris None      0 Apr  7  2015 sbin
drwxrwxrwt+  1 Chris None      0 Feb 15 22:02 tmp
drwxr-xr-x+  1 Chris None      0 Dec 27 14:16 usr
drwxr-xr-x+  1 Chris None      0 Dec  3  2014 var
Chris@Chris-PC:~/test$
  • Thank you! I was completely blanking on how to do that command. I was confused with $* –  A Pompeii Feb 16 '16 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.