How to Set Default Arguments for "Ls" in Linux

How can I set default arguments for ls in Linux?

You could just alias ls itself. So something like:

alias ls='ls -ahl'

How to write a bash script that takes optional input arguments?

You could use the default-value syntax:

somecommand ${1:-foo}

The above will, as described in Bash Reference Manual - 3.5.3 Shell Parameter Expansion [emphasis mine]:

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

If you only want to substitute a default value if the parameter is unset (but not if it's null, e.g. not if it's an empty string), use this syntax instead:

somecommand ${1-foo}

Again from Bash Reference Manual - 3.5.3 Shell Parameter Expansion:

Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

what does mean that mandatory arguments to long options are mandatory for short options too

On Linux, it's often possible to specify the same command-line option as either a long option (that is, the form with -- and a word) or a short option (the form with - and a letter). For example, with ls, you can specify ls -a or ls --all, and they are completely equivalent.

Some options are specified to take an argument. For example, when you use the -w or --width argument to ls, you must tell it how wide you want the text to be. In this case, the argument is mandatory. This text is telling you that although the manual page doesn't list an argument for -w, only for --width, it is required for both and has the same meaning. Putting this text in the manual page simply avoids duplicating a lot of needless text and makes the manual page tidier and easier to read.

There are also optional arguments, such as the argument to --color. In such a case, if you fail to specify it, some default value (in this case, always) is assumed.

Display Only Files And Dotfiles In The Default ls Command Format?

to list only the dot files in the current directory

find .  -maxdepth 1 -type f -name ".*" | xargs ls --color=tty

to list only the other files

find .  -maxdepth 1 -type f -not -name ".*" | xargs ls --color=tty

How to set a default value for command line parameter for git alias?

You want ${1--1} here, I think.

Explanation

Git aliases are run by a POSIX-style shell (sh). POSIX shell has:

${variable-default}

and:

${variable:-default}

as expansion options, where if $variable is not set, you get the default instead. The :- variant, which looks a bit like one of those old-style emoticons, means "use the default if the variable is set but expands to the empty string".

In most cases we use variable names here, e.g.:

somevar=somevalue

or:

anothervar=$(git ls-files --stage -- file.ext)

for instance, and then we'd have ${somevar-default}. But $1 is the first positional parameter, $2 is the second, and so on, and these work in these positions as well.

The default you want in this case is -1 so ${1--1}, which admittedly looks funny, works.

Note that Vogel612's comment was very close, as these shells do also have:

${variable=default}

and:

${variable:=default}

which behave similarly (expanding to the default if the variable is unset, and in the second case if the variable is set to the empty string as well) but also have the side effect of setting the variable. This is particularly useful when used with the no-op : (colon) command:

f() {
local name="$1" email="$2"
: ${name:="default name"} ${email:="<default.email@somewhere.com>"}
# now you can refer to "$name" and "$email";
# they are set and non-empty, even if someone passed the
# empty string as arguments 1 and/or 2.
}

(Unfortunately the local construct, while very widely available and generally a good idea, is not in POSIX sh. I was surprised to discover this recently. It keeps the variable from "escaping" the scope of the function. Fortunately, for one-off functions in Git aliases, it's not needed.)



Related Topics



Leave a reply



Submit