Various flags in bash [duplicate]
Asked Answered
C

2

11

I see many times flags such as if [-n ${Parameter}] or echo -n <string>. Sometimes, I have seen flags like -r and others used. However, I don't know how to search for the meaning of these flags on web. Could someone send me some link where I can understand as to what they mean or some general note as to how should I search for them on google? Thanks!

Crumby answered 20/7, 2020 at 15:41 Comment(3)
Read the doc?: man7.org/linux/man-pages/man1/bash.1.htmlProverbial
Search for "bash if statements". Here's the first result from Google: ryanstutorials.net/bash-scripting-tutorial/…Vulcan
There's also stuff like set -x (print trace of commands) and set -e (exit on first error/non-zero error code returning command)Tynan
M
6

You can of course check the doc on internet https://ss64.com/bash/if.html

Or if you have access to a linux or mac machine, just check out the installed doc !

Try man if for example.

Also, man could have multiple pages for the same query, for example man open will show the manual of openvt on my machine and is a command line executable. But writting man 2 open gives you the manual of the C open function. So by default man gives you manual of bash/command line and then C function. So man open could be written man 1 open

This is really useful if you don't have an internet access or if the version of the tools that you want to use is different from the "normal" one. I think about sed for example, that is different from linux and mac. So they has different manual.

Of course there is a man of man ... :) man man

I forgot to talk about help, most (and decent) program has the -h or/and --help. Most of the time the manual page shows much more information.

Mulloy answered 20/7, 2020 at 15:50 Comment(1)
On my Mac, man if brings up a help page for shell builtins, which doesn't provide the user with any obvious link to help about these flags.Fundus
S
1

A more straightforward answer:

 -a    file exists  This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.
 -b    file is a block device
 -c    file is a character device  [ -b "/dev/sda2" ]
 -d    file is a directory
 -e    file exists
 -f    file is a regular file (not a directory or device file)
 -G    group-id of file same as yours
 -g    set-group-id (sgid) flag set on file or directory
          If a directory has the sgid flag set, then a file created within that directory belongs to the group
          that owns the directory, not necessarily to the group of the user who created the file.
 -h    file is a symbolic link
 -k    sticky bit1 set
 -L    file is a symbolic link
 -N    file modified since it was last read
 -O    you are owner of file
 -p    file is a pipe  [ -p /dev/fd/0 ]
 -r    file has read permission (for the user running the test)
 -S    file is a socket
 -s    file is not zero size
 -t    file (descriptor) is associated with a terminal device.
 -u    set-user-id2 (suid) flag set on file
 -w    file has write permission (for the user running the test)
 -x    file has execute permission (for the user running the test)

 -nt   file f1 is newer than f2   f1 -nt f2
 -ot   file f1 is older than f2   f1 -ot f2
 -ef   files f1 and f2 are hard links to the same file   f1 -ef f2
    
 !     "not" -- reverses the sense of the tests above (returns true if condition absent).

Reference: https://ss64.com/bash/syntax-file-operators.html

Supervise answered 28/11, 2023 at 5:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.