0

Is there a key that explains theese letters such as -n, -d, etc all mean ?

Example:

if [[ -d ${directory_name} ]]; then ...; fi

What is the -d or where do I find a key?

Example:

while [[ -n ${variable_name} ]]; do ...; done

what is the -n mean??

Is there a guide that explains what all of these -n, -e, -a, -d, -s, -h, etc?

I do a lot of Unix shell scripting modifications, and I've seen these arguments used inside the loops or if then statements on variables and file locations etc.

I'm looking for a place that explains each of the arguments as mentioned above for scripting.

  • 1
    Search for conditional expressions in the man page of your shell, eg zsh – don_crissti Jan 11 '17 at 17:22
  • 2
    or man test, depending on your OS/distro (only if your shell is not providing a built-in test). – user4556274 Jan 11 '17 at 17:33
  • "Conditional Expressions" was what i was looking for in the man page. Thanks for your quick reply! – TechGuyChris Jan 11 '17 at 20:44
0

you want the TLDP page for BASH,

advanced bash: http://www.tldp.org/LDP/abs/html/index.html

beginner bash: http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html

what you were asking is about file test operators and can be found here: http://www.tldp.org/LDP/abs/html/fto.html

and also bash conditional expressions which can found here: http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html

-d  file is a directory
-f  file is a regular file
-e  file exists
-s  file size is not zero
-b  file is a block device
-h  file is a symbolic link
-w  file has write permissions for user executing this bash statement
{there are more}

#!/bin/bash
if [ -e $1 ] && [ -w $1 ]; then
   echo "the file you entered was "$1" and it exists and you have write permission to it"
else
   echo "condition failed for exist and for write permission"
fi
  • Wow!! Thanks for your help and the resources. Thats exactly what I wanted!!! Thanks much! – TechGuyChris Jan 11 '17 at 20:42
4

I believe you should find everything in details in the man page of your shell (e.g. man bash).

Moreover, bash, for one, has a builtin command named help. Just give the command in parameter. Here the command is [[ and help [[ redirects to the command test (a.k.a. [), therefore help test will give you what you need.

Finally, builtins often have their standalone counterparts in /bin and their are generally provided with a manpage => man [ or man test

  • Perfect!! This is what I needed!! I was originally doing "man -n" etc and getting nowhere. Thank you for providing the answer I needed. I wish I could mark this as the correct answer as well. – TechGuyChris Jan 11 '17 at 20:43
  • You can do a man xxx, but only on a command, not on an option. So, you must first identify the command which is the first word after if or while. For bash builtins, use help instead. – xhienne Jan 11 '17 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.