What is the meaning of `! -d` in this Bash command?

bash script
what does "-z" mean in bash
what does mean in bash
1 bash
bash "-n"
what does "-e" mean in bash
what does do in bash
bash reference

Simple question, I don't understand what the ! and -d in the below statement means.

if [ ! -d $directory ]

-d is a operator to test if the given directory exists or not.

For example, I am having a only directory called /home/sureshkumar/test/.

The directory variable contains the "/home/sureshkumar/test/"

if [ -d $directory ]

This condition is true only when the directory exists. In our example, the directory exists so this condition is true.

I am changing the directory variable to "/home/a/b/". This directory does not exist.

if [ -d $directory ]

Now this condition is false. If I put the ! in front if the directory does not exist, then the if condition is true. If the directory does exists then the if [ ! -d $directory ] condition is false.

The operation of the ! operator is if the condition is true, then it says the condition is false. If the condition is false then it says the condition is true. This is the work of ! operator.

if [ ! -d $directory ]

This condition true only if the $directory does not exist. If the directory exists, it returns false.

What does || mean in bash?, || is the OR operator. It executes the command on the right only if the command on the left returned an error. See Confusing use of && and || operators. -depends on the command and how it responds to it. but it usually means the stdout/stdin in bash commands. In this case, -is the argument to the -O option, so the downloaded data is not saved in a file, but printed to stdout, so it can be piped to the tar command

The brackets are the test executable, the exclamation mark is a negation, and the -d option checks whether the variable $directory is a directory.

From man test:

       FILE exists and is a directory

       EXPRESSION is false

The result is an if statement saying "if $directory is not a directory"

Is it possible to define a command in bash?, just type: alias gb='cd /media/Dan/evolution'. To make this setting permanent (so that it sticks after you restart or open another console) add this� "-" is a command line argument with no specific bash function. Exactly how it is handled is dependent on the command being run (in this case apt-key ). Depending on context, it's typically used to indicate either " read data from stdin rather than from a file ", or " process the remainder of the line as data rather than as command line arguments ".

! means not

-d means test if directory exists

So, if [ ! -d $directory ] means if $directory does not exist, or $directory isn't a directory (maybe a file instead).

Usually this is followed by a statement to create the directory, such as

if [ ! -d $directory ]; then
  mkdir $directory

command line - What does the "-" in "bash -" mean?, When in doubt, read the source code. =) Bash 4.3, shell.c line 830, in function parse_shell_options() : /* A single `-' signals the end of options. command 2>&1 > file . Another way to redirect stderr to stdout is to use the &> construct. In Bash &> has the same meaning as 2>&1: command &> file Conclusion # Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout, use the 2>&1 or &> constructs.

-d is a test operator in bash and when you put ! before test operator - its negating the same


Bash (Bourne-Again Shell) Definition, Bash (bash) supports all the commands of the original Bourne shell (sh), as well as many others. It also includes features from the Korn shell� The builtin command makes sure you run the shell built-in version of the command rather than running another command with the same name. For example, imagine you defined a shell function named cd to print some extra status every time you change directories, but you messed it up and now you cannot change directories correctly.

Bash in Linux: Definition & Overview, Script. Apart from the commands you enter, bash can run a set of commands from a file, called a shell script, which we will refer to� Bash nohup command tutorial. The meaning of nohup is ‘no hangup‘. Normally, when we log out from the system then all the running programs or processes are hangup or terminated. If you want to run any program after log out or exit from Linux operating system then you have to use nohup command.

What is Bash?, A shell program is typically an executable binary that takes commands that you conventions, logic, and histories meaning they can look similar, however, can� Bash Reference Manual Bash Manual. Grouping Commands. Bash provides two ways to group a list of commands to be executed as a unit. When commands are grouped, redirections may be applied to the entire command list. For example, the output of all the commands in the list may be redirected to a single stream. ( list )

Understanding Bash: Elements of Programming, This shows that Bash splits the input into words, then attempts to execute the first word as a command (the "words" VAR and 9 ). Here� A script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell. /bin/sh is an executable representing the system shell . Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell.

Making Linux Work: Essential Tips & Techniques, Tip 2.2.8: Creating Command Aliases in Bash The Bash shell lets you define custom commands known as aliases. These aliases can make it quicker and easier� [code]#!/bin/bash [/code]This is known as shebang , it tells the interpreter that the following lines are written for bash , so execute this file as bash script.

  • [ is also the test command, so read this, as "if test NOT directory $dir" ... then do something. Good luck.
  • aside: needs to be [ ! -d "$directory" ] to be correct; without the quotes, it'll behave badly if your directory name has spaces or can be expanded as a glob.
  • BTW, next time you have some shell you don't understand, try copying-and-pasting it into explainshell.com (albeit, in this case, either without the if, or adding a ; then : "something here"; fi at the end to make the syntax correct).
  • Thanks for explainshell.com. So far, I knew only about shellcheck.net
  • Note that the -d test also fails if the named file exists but is a regular file, or is something else more esoteric (FIFO, block special, character special, etc). If the name is a symlink and points to a directory, it passes; otherwise it fails.
  • well -- whether the result of string-splitting and glob-expanding the variable directory's contents names a (single) directory, rather. If you want to check whether those contents correspond with a single directory name when interpreted literally, without globbing or string-splitting first applied, more quotes would be needed.
  • mkdir $directory won't work well if your directory already exists as a file. And if the concern is whether it doesn't exist at all, mkdir -p "$directory" with no preceding test is the best-practice idiom to handle the case.
  • ...failing to quote the expansion, by contrast, is just plain flat-out wrong. If having assigned directory="My Documents" you run mkdir $directory, you get two directories created, one named My and the other named Documents, and [ ! -d $directory ] becomes equivalent to [ ! -d "My" "Directory" ] -- syntactically invalid, since, -d expects only one argument to follow, not two.