Hot answers tagged

174

You were pretty close with your example. It works just fine when you use it with arguments such as these. Sample script: $ more ex.bash #!/bin/bash echo $1 $2 Example that works: $ ssh serverA "bash -s" < ./ex.bash "hi" "bye" hi bye But it fails for these types of arguments: $ ssh serverA "bash -s" < ./ex.bash "--time" "bye" bash: --: invalid ...


91

The a* and *a* syntax is implemented by the shell, not by the ls command. When you type ls a* at your shell prompt, the shell expands a* to a list of existing all files in the current directory whose names start with a. For example, it might expand a* to the sequence a1 a2 a3, and pass those as arguments to ls. The ls command itself never sees the * ...


73

$ touch ./-c $'a\n12\tb' foo $ du -hs * 0 a 12 b 0 foo 0 total As you can see, the -c file was taken as an option to du and is not reported (and you see the total line because of du -c). Also, the file called a\n12\tb is making us think that there are files called a and b. $ du -hs -- * 0 a 12 b 0 -c 0 foo ...


71

This is a POSIX requirement for all utilities, see POSIX chapter 12.02, Guideline 10 for more information: The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character. POSIX recommends all utilities ...


68

Its purpose is to provide access to the various matchers implemented in GNU grep in one form or another, in particular AWK matchers which aren’t available otherwise, probably for testing purposes (see bug 16481 which discusses adding the gawk and posixawk matchers). However it is currently buggy, which is the reason why it’s documented as being undocumented:...


67

With symlinks, tools have two things they can do: Treat the symlink as a symlink ("preserving its nature"), or Treat the symlink as the type of file that it points to. Saying that -H "preserves its nature" is not a contradiction. Consider the alternative. If you use -L, any symlinks cp finds will be opened, and their contents copied to the target file ...


46

- is defined in POSIX Utility Syntax Guidelines as standard input: Guideline 13: For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -. You can see ...


37

What's the problem First, like for many utilities, you'll have an issue with file names starting with -. While in: sh -c 'inline sh script here' other args The other args are passed to the inline sh script; with the perl equivalent, perl -e 'inline perl script here' other args The other args are scanned for more options to perl first, not to the inline ...


36

cd - is actually shorthand for cd "$OLDPWD" && pwd, where $OLDPWD is set each time you change directories to the directory you were just in. Handling of - depends on the application. Some applications use - to signify STDIN, e.g. grep, awk Other applications may use - as a shorthand for anything they choose, as Michael's answer specifies, with su, -...


33

The most robust way to code that is to use an array: wget_options=( --mirror --no-host-directories --directory_prefix="$1" ) wget "${wget_options[@]}" "$2/$3"


31

When bash is called with the name sh, it does this: if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0') act_like_sh++; and then later sets the POSIXLY_CORRECT shell variable to y: if (act_like_sh) { bind_variable ("POSIXLY_CORRECT", "y", 0); sv_strict_posix ("POSIXLY_CORRECT"); } bind_variable calls ...


28

Basically, you should double quote variable expansions to protect them from word splitting (and filename generation). However, in your example, wget_options='--mirror --no-host-directories' wget $wget_options --directory_prefix="$local_root" "$remote_root$relative_path" word splitting is exactly what you want. With "$wget_options" (quoted), wget doesn't ...


27

See Keith Thompson’s answer; but to explain why ls --directory a* shows files and directories: The --directory option does not suppress non-directory files. Instead, it lists the directories as such, while it would otherwise list their content. Example: $ mkdir foo $ touch foo/bar $ ls foo bar $ ls --directory foo foo


26

getopt vs getopts seems to be a religious issue. As for the arguments against getopt in the Bash FAQ: "getopt cannot handle empty arguments strings" seems to refer to a known issue with optional arguments, which it looks like getopts doesn't support at all (at least from reading help getopts for Bash 4.2.24). From man getopt: getopt(3) can parse long ...


24

If you use less as pager for man you can try LESS="+/^\s+-b" man wget where + symbol to execute next operation after less has opened / command to start search ^\s+-b regexp to match -b from start of line So if you like you can arrange the apropriate function for shell function rman { #USAGE: rman programm.name option.to.search (with "-" symbol) LESS="+/...


23

When using rm with both -i and -f options, the first one will be ignored. This is documented in the POSIX standard: -f Do not prompt for confirmation. Do not write diagnostic messages or modify the exit status in the case of nonexistent operands. Any previous occurrences of the -i option shall be ignored. -i Prompt for ...


22

The POSIX utility syntax guidelines (specifically #13) specify that for utilities that expect a file name to read from, - means standard input, and for utilities that expect a file name to write to, - means standard output. For example, cat somefile - copies the content of somefile to its standard output, followed by what it reads on its standard input. ...


21

getopts processes the options in turn. That's its job. If the user happens to pass no option, the first invocation of getopts exits the while loop. If none of the options take an argument, the value of OPTIND indicates how many options were passed. In general, OPTIND is the number of arguments that are options or arguments to options, as opposed to non-...


21

The difference in behavior between -L and -H comes when -r is specified as well. cp won't create symlinks in subdirectories with -L -r but it will if you use -H -r.


21

Because it's the right thing to do. Suppose you had a script doing something like: ls $LS_OPTIONS -l "$dir" where it's possible that $LS_OPTIONS already contains -l. It would be counter-intuitive and annoying for this command to produce an error and would require extra logic in the script to avoid it. -l may not be the best example for this, but hopefully ...


21

You only need defaults if the field would otherwise be empty. You can leave out the options field altogether if it's empty, unless the 5th or 6th fields are present. Field 5 is the dump frequency, rarely used nowadays. Field 6 fsck order, should be 1 for /, 2 for other filesystems mounted on boot and 0 otherwise. Fields 5 and 6 can be omitted if their value ...


20

You cannot pass two arguments with single option using getopts. I recommend the following alternatives: Put quotes around multiple arguments In this case getopts will treat them as one argument, but you will be able to split it later on. You can even put all arguments in the array at once: #!/bin/bash while getopts ":hr:l:" opt; do case $opt in ...


20

You can remove the first 12 lines with: tail -n +13 (That means print from the 13th line.) Some implementations of head like GNU head support: head -n -12 but that's not standard. tail -r file | tail -n +12 | tail -r would work on those systems that have tail -r (see also GNU tac) but is sub-optimal. Where n is 1: sed '$d' file You can also do: ...


20

ls -alh is the same as ls -a -l -h. Multiple short options can be combined like this. Here are the meanings of those options from man ls: -a, --all do not ignore entries starting with . -l use a long listing format -h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G)


19

Though Michael mentions that su and other applications can use - to mean whatever they want (read from stdin is common), Git does use - in a fashion similar to how cd does, for changing branches. $ git status On branch master $ git checkout foobar $ git status On branch foobar $ git checkout - $ git status On branch master


18

When you run this script without any options, getopt will return false, so it won't enter the loop at all. It will just drop down to the print - is this ksh/zsh? If you must have an option, you're best bet is to test $name after the loop. if [ -z "$name" ] then usage exit fi But make sure $name was empty before calling getopts (as there could have ...


18

Any program can use - as an argument, to mean whatever they want. One common example is su, which uses - as shorthand for --login. The only convention I can think of is that programs that read from files frequently use - to mean "read from stdin", but it's entirely up to the program


18

There are no consistent definitions of the terms "option", "argument", and "flag", and there is no central authority within the software development world that could enforce their usage. This happens with much terminology: after 30+ years of using the word "directory", I now have to deal with people using the word "folder" who have been confused by Microsoft'...


18

Use -- to indicate end of options for ls: ls -- -* or do the following to explicitly indicate the argument on current directory with ./ ls ./-* If you want to input more options for ls, put them before -- or ./ e.g. ls -l -- -* ls -l ./-*


17

Most commands that accept --foo as an option also accept -- by itself as an "end of options, start of arguments" marker - so you could do: printf -- "--no-color\n--format-doc\n--no-profile\n" >> ~/.rspec-test But the more specific answer to your exact example is that the first argument to printf is a format specifier, and you're making things more ...


Only top voted, non community-wiki answers of a minimum length are eligible