0

I ran across the following Bash function which was suggested as a useful alias to add to the .bashrc file. It lists the last 13 files in a directory that were modified.

I don't understand what is being done with the ${1:-.} argument, though. It looks like some kind of substring extraction, but I couldn't find the meaning of -. in the Advanced Bash Scripting Manual.

I tried the command in a few directories and didn't notice any difference between the output when I removed this argument. My guess is that it's there to prevent an error when encountering some specific type of file or file name. What is it doing? And what is the purpose of including it in the command?

function lst()
{
ls -lashtg ${1:-.} | head -13
}
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
swimfar2
  • 103
  • 8
  • `I couldn't find the meaning of -. in the Advanced Bash Scripting Manual.` it's here: https://tldp.org/LDP/abs/html/parameter-substitution.html , but you should rather see bash manual - tldp tends to be outdated. – KamilCuk Jun 12 '21 at 00:06
  • https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html – Shawn Jun 12 '21 at 00:34
  • This is not a duplicate question. But @BenjaminW. answered it below so I'm fine with keeping it closed. – swimfar2 Jun 14 '21 at 18:42
  • How is it not a duplicate of `:-` in parameter expansions? Notice that `-.` is not special; the `-` is part of the `:-`. – Benjamin W. Jun 14 '21 at 18:58

1 Answers1

1

$1 is the first command line argument. Im sure you know.

${1:-.} simply puts a . when no first line argument is given.

Thus

lst

Translates to

ls -lashtg . | head -13

It would workout without the substitution I guess. But I also guess that this is just there as a best practice

Leon
  • 35
  • 7
  • 1
    It's there so you can optionally provide a directory, `lst path/to/somedir`, but if you don't, it uses the current directory `.` instead. The expansion should be quoted, though. – Benjamin W. Jun 12 '21 at 00:12