4644
Comment: comment5 http://kira0332.007webs.com/mint-salad-recipe.html mozerella salad recipe http://kira0332.007webs.com/n-awlins-bbq-shrimp-recipe.html potted shrimps recipe http://kira0332.007webs.com/c
|
20902
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
comment5 http://kira0332.007webs.com/mint-salad-recipe.html mozerella salad recipe http://kira0332.007webs.com/n-awlins-bbq-shrimp-recipe.html potted shrimps recipe http://kira0332.007webs.com/cheap-air-tickets-new-york.html cheep air line tickets http://kira0332.007webs.com/good-pizza-dough-recipe.html great pizza recipe http://kira0332.007webs.com/comedogenic-hair-care.html david babaii hair care http://kira0332.007webs.com/vegetable-soup-recipe-in.html vegetable suop recipe http://kira0332.007webs.com/coconut-shrimp-dipping-sauce-recipe.html easy shrimp alfredo recipes http://kira0332.007webs.com/recipe-chicken-fettucine-alfredo.html recipe for apricot chicken http://kira0332.007webs.com/travel-insurance-for-oap.html travel insurance for over70s http://kira0332.007webs.com/online-airticket-booking-sites.html online drowsy chaperone tickets http://kira0332.007webs.com/slow-cooker-recipes-chicken-drumsticks.html south indian chicken recipes http://kira0332.007webs.com/nadds-hair-removal.html no hair removal system http://kira0332.007webs.com/christian-book-distribution.html bereans christian book store http://kira0332.007webs.com/employment-law-qualifications.html employment law wiki http://kira0332.007webs.com/new-york-daily-news-mudwrestling-iraq.html sciencenow daily news 2 29 http://kira0332.007webs.com/used-car-dealerships-louisville.html used car resale values http://kira0332.007webs.com/read-book-of-mormon-online.html buy and read books online http://kira0332.007webs.com/jamie-oliver-steak-recipe.html mightfish steak recipe http://kira0332.007webs.com/sports-jobs-canada.html jobs oil and gas canada http://kira0332.007webs.com/south-australian-job-search.html jobs in australian immigration http://kira0332.007webs.com/laser-hair-removal-fda.html laser hair removal information http://kira0332.007webs.com/paris-airline-tickets.html airline tickets discounts http://kira0332.007webs.com/hitachi-magic-hand-massager.html hitachi hand held massager for sale http://kira0332.007webs.com/bus-travel-tickets.html cheap air tickets travel http://kira0332.007webs.com/cheapflight-tickets-com.html tickets cheap flights discount airfare cheap hotels http://kira0332.007webs.com/university-of-illinois-career-center.html job link career center http://kira0332.007webs.com/fulton-county-schools-career.html careerschoolaid com apply http://kira0332.007webs.com/cheap-plane-tickets-paris.html europe airplane tickets http://kira0332.007webs.com/vintage-hairstyle-photos.html vintage hairstyles 1950s http://kira0332.007webs.com/hairstyles-for-men-long-hair.html hairstyles for men with medium length http://kira0332.007webs.com/wild-mountain-water-park-mn.html wilderness resort indoor water park http://kira0332.007webs.com/new-styles-for-long-hair.html nicole kidman hairstyle http://kira0332.007webs.com/audio-book-for-harry-potter.html new harry potter book cost http://kira0332.007webs.com/valentine-days-gift-ideas.html valentinesday gift ideas http://kira0332.007webs.com/sport-chek-tickets.html student sports tickets http://kira0332.007webs.com/sierra-leone-newspapers-online.html arkansas online newspapers http://kira0332.007webs.com/immac-hair-removal-cream.html immac hair removal cream http://kira0332.007webs.com/fish-taco-recipe.html fish recipe indian http://kira0332.007webs.com/curly-hairstyles-pictures.html current boys hairstyles http://kira0332.007webs.com/long-wavy-hair-style.html looking for new hairstyles http://kira0332.007webs.com/management-books-bestsellers.html barns and noble book sellers http://kira0332.007webs.com/cheap-orlando-tickets.html cheap seaworld orlando tickets http://kira0332.007webs.com/new-kids-on-the-block-tickets-los-angeles.html dark knight tickets los angeles http://kira0332.007webs.com/srilankan-daily-news.html daily news tribune waltham ma http://kira0332.007webs.com/lemon-meringue-pie-recipe-uk.html a recipe for pecan pie http://kira0332.007webs.com/audio-book-in-french.html harry potter audio books uk http://kira0332.007webs.com/tushy-massage-parlor.html tulsa massage parlor reviews http://kira0332.007webs.com/wedding-hairstyles-side-ponytail.html weedding hair styles http://kira0332.007webs.com/midsummer-nights-dream-online-book.html online books library http://kira0332.007webs.com/ideas-for-engagement-party.html luau kids party ideas http://kira0332.007webs.com/vegetarian-potato-leek-soup-recipe.html vegetarian recipes around the world http://kira0332.007webs.com/coastal-management-newspaper-article.html newspaper articles on wildlife |
## page was renamed from BashGuide/01.CommandsAndArguments [[BashGuide|<- Contents]] | [[BashGuide/SpecialCharacters|Special Characters ->]] ---- <<TableOfContents>> <<Anchor(StartOfContent)>> = Commands and Arguments = [[BASH]] reads commands from its input (which is usually either a terminal or a file). Each line of input that it reads is treated as a ''command'' — an instruction to be carried out. (There are a few advanced cases, such as commands that span multiple lines, that will be gotten to later.) Bash divides each line into ''words'' that are demarcated by a whitespace character (spaces and tabs). The first word of the line is the name of the command to be executed. All the remaining words become ''arguments'' to that command (options, filenames, etc.). Assume we're in an empty directory... (to try these commands, create an empty directory called `test` and enter that directory by running: `mkdir test; cd test`): {{{ $ ls # List files in the current directory (no output, no files). $ touch a b c # Create files 'a', 'b', and 'c'. $ ls # List files again, and this time outputs: 'a', 'b', and 'c'. a b c }}} The command `ls` prints out the names of the files in the current directory. The first time we run the list command we get no output, because there are no files yet. The `#` character at the start of a word indicates a ''comment''. Any words following the comment are ignored by the shell, meant only for reading. If we run these examples in our own shell, we don't have to type the comments; but even if we do, the command will still work. `touch` is an application that changes the ''Last Modified'' time of a file. If the filename that it is given does not exist yet, it creates a file of that name as a new and empty file. In this example, we passed three arguments. `touch` creates a file for each argument. `ls` shows us that three files have been created. {{{ $ rm * # Remove all files in the current directory. $ ls # List files in the current directory (no output, no files). $ touch a b c # Create files 'a', 'b' and 'c'. $ ls # List files again; this time the outputs: 'a', 'b' and 'c'. a b c }}} `rm` is an application that removes all the files that it was given. `*` is a [[glob]]. It basically means ''all'' and in this case means all files in the current directory. We will talk more about globs later. Now, did we notice that there are several spaces between `a` and `b`, and only one between `b` and `c`? Also, notice that the files that were created by `touch` are no different than the first time. The amount of whitespace between arguments does not matter! This is important to know. For example: {{{ $ echo This is a test. This is a test. $ echo This is a test. This is a test. }}} `echo` is a command that prints its arguments to ''standard output'' (which in our case is the terminal). In this example, we provide the `echo` command with four arguments: 'This', 'is', 'a', and 'test.'. `echo` takes these arguments, and prints them out one by one with a space in between. In the second case, the exact same thing happens. The extra spaces make no difference. If we ''want'' the extra whitespace, we need to pass the sentence as one single argument. We can do this by using [[Quotes|quotes]]: {{{ $ echo "This is a test." This is a test. }}} Quotes group everything inside them into a single argument. The argument is: '`This is a test.`'... specifically spaced. Note that the quotes are not part of the argument — Bash removes them before handing the argument to `echo`. `echo` prints this single argument out just like it always does. Be very careful to avoid the following: {{{ $ ls # There are two files in the current directory. The secret voice in your head.mp3 secret $ rm The secret voice in your head.mp3 # Executes rm with 6 arguments; not 1! rm: cannot remove `The': No such file or directory rm: cannot remove `voice': No such file or directory rm: cannot remove `in': No such file or directory rm: cannot remove `your': No such file or directory rm: cannot remove `head.mp3': No such file or directory $ ls # List files in the current directory: It is still there. The secret voice in your head.mp3 # But your file 'secret' is now gone! }}} We need to make sure we quote filenames properly. If we don't, we'll end up deleting the wrong things! `rm` takes filenames as arguments. If our filenames have spaces and we do not quote them, Bash thinks each word is a separate argument. Bash hands each argument to `rm` separately. Like individually wrapped slices of processed cheese, `rm` treats each argument as a separate file. In the above example, `rm` tried to delete a file for each word in the filename of the song, rather than keeping the filename intact. That caused our file `secret` to be deleted, and our song to remain behind! This is what we should have done: {{{ $ rm "The secret voice in your head.mp3" }}} Arguments are separated from the command name and from each other by a whitespace. This is important to remember. For example, the following is '''wrong''': {{{ $ [-f file] }}} We want the command `[` (a synonym for test) to be separated from the arguments: `-f`, `file`, and `]`. If we do not separate `[` and `-f` from each other with a whitespace, Bash will think we are trying to execute a command named `[-f`. The arguments `file` and `]` will so need to be separated by spaces. Additionally, the command `[` requires a closing argument of `]`. The correct command separates all arguments with whitespaces: {{{ $ [ -f file ] }}} (We'll cover the `[` command in more detail later. We see a lot of people who are confused by this behavior; they think that they can omit the whitespace between it and its arguments, so we need to present this particular example early.) And, if our ''filename'' contains whitespace or other [[BashGuide/SpecialCharacters|special characters]], it should also be quoted: {{{ $ [ -f "my file" ] }}} Have a good look at [[Arguments]], [[Quotes]], WordSplitting and http://wiki.bash-hackers.org/syntax/words if all this isn't very clear yet. It is important to have a good grasp of how the shell interprets the whitespace and special characters before continuing with this guide. '''Review:''' * ''Arguments'': These are additional words specified after the command ('`ls -l foo`' executes `ls` with two arguments). * ''Quotes'': The two forms of quotes, single and double (`'` and `"`), are used to group words and can protect special characters. The difference between `'` and `"` will be discussed later. '''Additionally:''' * Tip — ''Always'' quote sentences or strings that belong together, even if it's not absolutely necessary. This developed practice will reduce the risk of human error in the scripts. (e.g. quoting a sentence of an `echo` command). * FAQ — I'm trying to put a command in a variable, but the complex cases always fail! [[BashFAQ/050]]. * FAQ — How can I handle command line arguments (options) to my script easily? [[BashFAQ/035]]. == Strings == The term ''string'' refers to a sequence of characters which is treated as a single unit. The term is used loosely throughout this guide, as well as in almost every other programming language. In Bash programming, almost ''everything'' is a string. When we type a command, the command's name is a string and each argument is a string; variable names are strings, and the contents of variables are strings as well; a filename is a string, and most files ''contain'' strings. They're everywhere! An entire command can also be considered a string. This is not normally a useful point of view, but it illustrates the fact that ''parts'' of strings can sometimes be considered strings in their own right. A string which is part of a larger string is called a ''substring''. Strings do not have any intrinsic meaning. Their meaning is defined by how and where they are used. Let's try another example. With the editor, write a shopping list and save it with the filename "list", and use `cat` to print it: {{{ $ cat list shampoo tissues milk (skim, not whole) }}} We typed a command: `cat list`. The shell reads this command as a string, and then divides it into the substrings `cat` and `list`. As far as the shell is concerned, `list` has no meaning, it's just a string with four characters in it. `cat` receives the argument `list`, which is a string of a filename. The string `list` has become meaningful because of ''how it was used''. The file happens to contain some text, which we see on our terminal. The entire file content — taken as a whole — is a string, but that string is not meaningful. However, if we divide the file into ''lines'' (and therefore treat each line as a separate string), then we see each individual ''line'' has meaning. We can divide the final line into words, but these words are not meaningful by themselves. We can't buy "`(skim`" at the store, and we might get the wrong kind of "`milk`". Dividing the lines into words is not a useful thing to do in this example. But the shell doesn't know any of this — only ''we'' do! So, when we are dealing with commands, data, and variables — all of which are just strings to the shell — we have ''all'' the responsibility. We need to be sure everything that needs to be separated is separated properly, and everything that needs to stay together stays together properly. We'll touch on these concepts repeatedly as we continue. == Types of commands == Bash understands several different types of commands: aliases, functions, builtins, keywords, and executables. === Aliases === An alias is a way of shortening a command. (They are only used in '''interactive''' shells and ''not'' in scripts — this is one of the ''very'' few differences between a script and an interactive shell.) An alias is a ''word'' that is mapped to a certain ''string''. Whenever that ''word'' is used as a command name, it is replaced by the ''string'' before executing the command. So, instead of executing: {{{ $ nmap -Pn -A --osscan-limit 192.168.0.1 }}} We could use an alias like this: {{{ $ alias nmapp="nmap -Pn -A --osscan-limit" $ nmapp 192.168.0.1 }}} Aliases are limited in power; the replacement only happens in the first word. To have more flexibility, use a function. Aliases are only useful as simple textual shortcuts. === Functions === Functions in Bash are somewhat like aliases, but more powerful. Unlike aliases, they can be used in '''scripts'''. A function contains shell commands, and acts very much like a small script; they can even take arguments and create local variables. When a function is called, the commands in it are executed. Functions will be covered in depth [[BashGuide/CompoundCommands#Functions|later in the guide]]. === Builtins === Bash has some basic commands built into it, such as: `cd` (change directory), `echo` (write output), and so on. They can be thought of as functions that are already provided. === Keywords === Keywords are like builtins, with the main difference being that special parsing rules apply to them. For example, `[` is a Bash builtin, while `[[` is a Bash keyword. They are both used for testing stuff. `[[` is a keyword rather than a builtin and is therefore able to offer an extended test: {{{ $ [ a < b ] -bash: b: No such file or directory $ [[ a < b ]] }}} The first example returns an error because Bash tries to redirect the file `b` to the command `[ a ]` (see [[BashGuide/InputAndOutput#File_Redirection|File Redirection]] for additional explanation). The second example actually does what we expect it to, alphabetical comparision. The character `<` no longer has its typical meaning of a ''File Redirection'' operator when it's used with the extended test `[[` keyword. === Executables === The last kind of command that can be executed by Bash is an ''executable''. (Executables may also be called ''external commands'' or ''applications''.) Executables are commonly invoked by typing only their name. This can be done because a pre-defined variable makes known to Bash a list of common, executable, file paths. This variable is called `PATH`. It is a set of directory names separated by colons (e.g.`/usr/bin:/bin`). When a command is specified (e.g. `myprogram`, or `ls`) without a file path (and it isn't an alias, function, builtin or keyword), Bash searches through the directories in `PATH`. The search is done in order, from left to right, to see whether they contain an executable of the command typed. If the executable is ''outside'' a known path... the executables file path will need to be defined. For an executable in the current directory, use `./myprogram`; if it's in the directory `/opt/somedirectory`, use `/opt/somedirectory/myprogram`. '''Review:''' * ''Alias'' — a word that is mapped to a string. Whenever that word is used as a command, it is replaced by the string it has mapped. * ''Function'' — a name that is mapped to a set of commands. Whenever the function is used as a command, it is called with the arguments proceding it. Functions are the basic method of making new commands. * ''Builtin'' — certain commands have been built into Bash. These are handled directly by the Bash executable and do not create a new process. * ''Executable'' — a program that can be executed by referring to its file path (e.g. `/bin/ls`), or simply by its name if its location is in the `PATH` variable. '''Additionally:''' * Manual — [[http://www.gnu.org/software/bash/manual/bashref.html#Simple-Commands|Simple Commands]] * FAQ — What is the difference between `test`, `[` and `[[`? [[BashFAQ/031]]. * FAQ — How can I make an alias that takes an argument? [[BashFAQ/080]]. * Tip — The `type` command can be used to get a description of the command type: {{{ $ type rm rm is hashed (/bin/rm) $ type cd cd is a shell builtin }}} == Scripts == A script is basically a sequence of commands in a file. Bash reads the file and processes the commands '''in order'''. It moves on to the next command only when the current one has '''ended'''. (The exception being if a command has been specified to run asynchronously, in the background. Don't worry too much about the this case yet — we'll learn about how that works later on.) Virtually any example that exists on the command line in this guide can also be used in a script. Making a script is easy. Begin by making a new file, and put this on the first line: {{{#!text #!/usr/bin/env bash }}} The header is called an ''interpreter directive'' (it is also called a ''hashbang'' or ''shebang''). It makes sure that whenever our script is executed, Bash will be used as its interpreter. The way it works is this: when the kernel executes a non-binary file, it reads the first line of the file; if the line begins with `#!`, the kernel uses the line to determine the interpreter to relay the file to. (There are other valid ways to do this as well, see below.) The `#!` must be at the very start of the file, with no spaces or blank lines before it. Our script's commands will appear on separate lines below this. '''Please''' do not be fooled by scripts or examples on the Internet that use `/bin/sh` as the interpreter. '''`sh` is not `bash`'''! Bash itself is a "sh-compatible" shell (meaning that it can run most 'sh' scripts and carries much of the same syntax) however, the opposite is not true; some features of Bash will break or cause unexpected behavior in `sh`. Also, please refrain from giving scripts a `.sh` extension. It serves no purpose, and it's completely misleading (since it's going to be a `bash` script, not an `sh` script). {{{#!wiki comment Some reasons for using ".sh": * It is more specific than not using any extension. * Most people probably think of it as short for "shell" rather than "Bourne or POSIX equivalent shell." * It's a convention. Following conventions is useful to ''at least'' everybody except the original developer(s). * Obscure file systems (not counting FAT) where there can't be more than three letters in the file extension. * People don't use ".bash3" (or, dog forbid, ".bash4.2.45(2)-release-x86_64-unknown-linux-gnu") even if it describes their interpreter more than the standard extensions. }}} ''It is perfectly fine to use '''Windows''' to write scripts. Avoid, however, using '''Notepad'''. "Microsoft Notepad" can only make files with DOS-style line-endings. DOS-style line-endings end with two characters: a '''Carriage Return''' and a '''Newline''' character. Bash understands line-endings with only '''Newline''' characters. As a result, the '''Carriage Return''' character will cause an unexpected surprise if one doesn't know it's there (very weird error messages). If at all possible, use a more capable editor like [[http://www.vim.org/|Vim]], [[http://www.gnu.org/software/emacs/|Emacs]], kate, GEdit... If one doesn't, the carriage returns will need to be removed from the scripts before running them.'' Once the script file has been created, it can be executed by doing: {{{ $ bash myscript }}} In this example, we execute `bash` and tell it to read the script. When we do it like this, the `#!` line is just a comment, Bash does not do anything at all with it. Alternatively, we can give our scripts executable permissions. With this method, instead of calling Bash manually, we can execute the script as an application: {{{ $ chmod +x myscript $ ./myscript }}} When executed in this way, the `#!` line tells the operating system (OS) what interpreter to use. The OS runs `/usr/bin/env`, which in turn runs `bash`, which reads our script. To decide where to put the script, a couple alternatives exists: generally people like to keep their scripts in a personally-defined directory; a few, like to keep their scripts in an existent, executable `PATH`. To use a personal directory: {{{ $ mkdir -p "$HOME/bin" $ echo 'PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc" $ source "$HOME/.bashrc" }}} The first command will make a directory called `bin` in the home, local, directory. It is traditional for directories that contain commands to be named `bin`, even when those commands are scripts and not compiled ''binary'' programs. The second command will add the directory's file path to the previous PATH variable and add that to the Bash configuration file (`.bashrc`). Every new instance of Bash will now check for executable scripts in defined directory. The third line reloads the Bash configuration file. (Some people prefer to use a different directory to hold their personal scripts, such as `$HOME/.config/bin` or `$HOME/.local/bin`. You can use whatever you prefer, as long as you are consistent.) Changes to Bash configuration file will not have an immediate effect, we have to make the step to re-read the file. In the example above, we used `source "$HOME/.bashrc"`, we could have also used `exec bash`, or we could close the existing terminal and open a new one. Bash would then initialize itself again by reading `.bashrc` (and possibly other files). In any case, we can now put our script in our `bin` directory and execute it as a normal command — we no longer need to prepend our script's name with the file path (which was the "`./`" part in the previous example): {{{ $ mv myscript "$HOME/bin" $ myscript }}} '''Additionally:''' * Tip — If the system has multiple versions of bash installed, specifying the interpreter by absolute path may be desirable in order to ensure the right bash version is used. For example: `#!/usr/bin/bash`. Type "`type -a bash`" to print the paths to all Bash executables in `PATH`. * Tip — The interpreter can optionally be followed by one word of interpreter's options. For example, the following options will turn on verbose debugging: "`#!/usr/bin/bash -xv`". To learn more, see [[BashGuide/Practices#Debugging|Debugging]]. * Tip — After defining the interpreter in the header, it is recommended to summarize the scripts purpose. If desired, copyright information can be listed...: {{{#!highlight bash #!/usr/bin/env bash # scriptname - a short explanation of the scripts purpose. # # Copyright (C) <date> <name>... # # scriptname [option] [argument] ... }}} <<Anchor(EndOfContent)>> -------- [[BashGuide|<- Contents]] | [[BashGuide/SpecialCharacters|Special Characters ->]] |
<- Contents | Special Characters ->
Contents
Commands and Arguments
BASH reads commands from its input (which is usually either a terminal or a file). Each line of input that it reads is treated as a command — an instruction to be carried out. (There are a few advanced cases, such as commands that span multiple lines, that will be gotten to later.)
Bash divides each line into words that are demarcated by a whitespace character (spaces and tabs). The first word of the line is the name of the command to be executed. All the remaining words become arguments to that command (options, filenames, etc.).
Assume we're in an empty directory... (to try these commands, create an empty directory called test and enter that directory by running: mkdir test; cd test):
$ ls # List files in the current directory (no output, no files). $ touch a b c # Create files 'a', 'b', and 'c'. $ ls # List files again, and this time outputs: 'a', 'b', and 'c'. a b c
The command ls prints out the names of the files in the current directory. The first time we run the list command we get no output, because there are no files yet.
The # character at the start of a word indicates a comment. Any words following the comment are ignored by the shell, meant only for reading. If we run these examples in our own shell, we don't have to type the comments; but even if we do, the command will still work.
touch is an application that changes the Last Modified time of a file. If the filename that it is given does not exist yet, it creates a file of that name as a new and empty file. In this example, we passed three arguments. touch creates a file for each argument. ls shows us that three files have been created.
$ rm * # Remove all files in the current directory. $ ls # List files in the current directory (no output, no files). $ touch a b c # Create files 'a', 'b' and 'c'. $ ls # List files again; this time the outputs: 'a', 'b' and 'c'. a b c
rm is an application that removes all the files that it was given. * is a glob. It basically means all and in this case means all files in the current directory. We will talk more about globs later.
Now, did we notice that there are several spaces between a and b, and only one between b and c? Also, notice that the files that were created by touch are no different than the first time. The amount of whitespace between arguments does not matter! This is important to know. For example:
$ echo This is a test. This is a test. $ echo This is a test. This is a test.
echo is a command that prints its arguments to standard output (which in our case is the terminal). In this example, we provide the echo command with four arguments: 'This', 'is', 'a', and 'test.'. echo takes these arguments, and prints them out one by one with a space in between. In the second case, the exact same thing happens. The extra spaces make no difference. If we want the extra whitespace, we need to pass the sentence as one single argument. We can do this by using quotes:
$ echo "This is a test." This is a test.
Quotes group everything inside them into a single argument. The argument is: 'This is a test.'... specifically spaced. Note that the quotes are not part of the argument — Bash removes them before handing the argument to echo. echo prints this single argument out just like it always does.
Be very careful to avoid the following:
$ ls # There are two files in the current directory. The secret voice in your head.mp3 secret $ rm The secret voice in your head.mp3 # Executes rm with 6 arguments; not 1! rm: cannot remove `The': No such file or directory rm: cannot remove `voice': No such file or directory rm: cannot remove `in': No such file or directory rm: cannot remove `your': No such file or directory rm: cannot remove `head.mp3': No such file or directory $ ls # List files in the current directory: It is still there. The secret voice in your head.mp3 # But your file 'secret' is now gone!
We need to make sure we quote filenames properly. If we don't, we'll end up deleting the wrong things! rm takes filenames as arguments. If our filenames have spaces and we do not quote them, Bash thinks each word is a separate argument. Bash hands each argument to rm separately. Like individually wrapped slices of processed cheese, rm treats each argument as a separate file.
In the above example, rm tried to delete a file for each word in the filename of the song, rather than keeping the filename intact. That caused our file secret to be deleted, and our song to remain behind!
This is what we should have done:
$ rm "The secret voice in your head.mp3"
Arguments are separated from the command name and from each other by a whitespace. This is important to remember. For example, the following is wrong:
$ [-f file]
We want the command [ (a synonym for test) to be separated from the arguments: -f, file, and ]. If we do not separate [ and -f from each other with a whitespace, Bash will think we are trying to execute a command named [-f. The arguments file and ] will so need to be separated by spaces. Additionally, the command [ requires a closing argument of ]. The correct command separates all arguments with whitespaces:
$ [ -f file ]
(We'll cover the [ command in more detail later. We see a lot of people who are confused by this behavior; they think that they can omit the whitespace between it and its arguments, so we need to present this particular example early.)
And, if our filename contains whitespace or other special characters, it should also be quoted:
$ [ -f "my file" ]
Have a good look at Arguments, Quotes, WordSplitting and http://wiki.bash-hackers.org/syntax/words if all this isn't very clear yet. It is important to have a good grasp of how the shell interprets the whitespace and special characters before continuing with this guide.
Review:
Arguments: These are additional words specified after the command ('ls -l foo' executes ls with two arguments).
Quotes: The two forms of quotes, single and double (' and "), are used to group words and can protect special characters. The difference between ' and " will be discussed later.
Additionally:
Tip — Always quote sentences or strings that belong together, even if it's not absolutely necessary. This developed practice will reduce the risk of human error in the scripts. (e.g. quoting a sentence of an echo command).
FAQ — I'm trying to put a command in a variable, but the complex cases always fail! BashFAQ/050.
FAQ — How can I handle command line arguments (options) to my script easily? BashFAQ/035.
Strings
The term string refers to a sequence of characters which is treated as a single unit. The term is used loosely throughout this guide, as well as in almost every other programming language.
In Bash programming, almost everything is a string. When we type a command, the command's name is a string and each argument is a string; variable names are strings, and the contents of variables are strings as well; a filename is a string, and most files contain strings. They're everywhere!
An entire command can also be considered a string. This is not normally a useful point of view, but it illustrates the fact that parts of strings can sometimes be considered strings in their own right. A string which is part of a larger string is called a substring.
Strings do not have any intrinsic meaning. Their meaning is defined by how and where they are used.
Let's try another example. With the editor, write a shopping list and save it with the filename "list", and use cat to print it:
$ cat list shampoo tissues milk (skim, not whole)
We typed a command: cat list. The shell reads this command as a string, and then divides it into the substrings cat and list. As far as the shell is concerned, list has no meaning, it's just a string with four characters in it. cat receives the argument list, which is a string of a filename. The string list has become meaningful because of how it was used.
The file happens to contain some text, which we see on our terminal. The entire file content — taken as a whole — is a string, but that string is not meaningful. However, if we divide the file into lines (and therefore treat each line as a separate string), then we see each individual line has meaning.
We can divide the final line into words, but these words are not meaningful by themselves. We can't buy "(skim" at the store, and we might get the wrong kind of "milk". Dividing the lines into words is not a useful thing to do in this example. But the shell doesn't know any of this — only we do!
So, when we are dealing with commands, data, and variables — all of which are just strings to the shell — we have all the responsibility. We need to be sure everything that needs to be separated is separated properly, and everything that needs to stay together stays together properly. We'll touch on these concepts repeatedly as we continue.
Types of commands
Bash understands several different types of commands: aliases, functions, builtins, keywords, and executables.
Aliases
An alias is a way of shortening a command. (They are only used in interactive shells and not in scripts — this is one of the very few differences between a script and an interactive shell.) An alias is a word that is mapped to a certain string. Whenever that word is used as a command name, it is replaced by the string before executing the command. So, instead of executing:
$ nmap -Pn -A --osscan-limit 192.168.0.1
We could use an alias like this:
$ alias nmapp="nmap -Pn -A --osscan-limit" $ nmapp 192.168.0.1
Aliases are limited in power; the replacement only happens in the first word. To have more flexibility, use a function. Aliases are only useful as simple textual shortcuts.
Functions
Functions in Bash are somewhat like aliases, but more powerful. Unlike aliases, they can be used in scripts. A function contains shell commands, and acts very much like a small script; they can even take arguments and create local variables. When a function is called, the commands in it are executed. Functions will be covered in depth later in the guide.
Builtins
Bash has some basic commands built into it, such as: cd (change directory), echo (write output), and so on. They can be thought of as functions that are already provided.
Keywords
Keywords are like builtins, with the main difference being that special parsing rules apply to them. For example, [ is a Bash builtin, while [[ is a Bash keyword. They are both used for testing stuff. [[ is a keyword rather than a builtin and is therefore able to offer an extended test:
$ [ a < b ] -bash: b: No such file or directory $ [[ a < b ]]
The first example returns an error because Bash tries to redirect the file b to the command [ a ] (see File Redirection for additional explanation). The second example actually does what we expect it to, alphabetical comparision. The character < no longer has its typical meaning of a File Redirection operator when it's used with the extended test [[ keyword.
Executables
The last kind of command that can be executed by Bash is an executable. (Executables may also be called external commands or applications.) Executables are commonly invoked by typing only their name. This can be done because a pre-defined variable makes known to Bash a list of common, executable, file paths. This variable is called PATH. It is a set of directory names separated by colons (e.g./usr/bin:/bin). When a command is specified (e.g. myprogram, or ls) without a file path (and it isn't an alias, function, builtin or keyword), Bash searches through the directories in PATH. The search is done in order, from left to right, to see whether they contain an executable of the command typed.
If the executable is outside a known path... the executables file path will need to be defined. For an executable in the current directory, use ./myprogram; if it's in the directory /opt/somedirectory, use /opt/somedirectory/myprogram.
Review:
Alias — a word that is mapped to a string. Whenever that word is used as a command, it is replaced by the string it has mapped.
Function — a name that is mapped to a set of commands. Whenever the function is used as a command, it is called with the arguments proceding it. Functions are the basic method of making new commands.
Builtin — certain commands have been built into Bash. These are handled directly by the Bash executable and do not create a new process.
Executable — a program that can be executed by referring to its file path (e.g. /bin/ls), or simply by its name if its location is in the PATH variable.
Additionally:
Manual — Simple Commands
FAQ — What is the difference between test, [ and [[? BashFAQ/031.
FAQ — How can I make an alias that takes an argument? BashFAQ/080.
Tip — The type command can be used to get a description of the command type:
$ type rm rm is hashed (/bin/rm) $ type cd cd is a shell builtin
Scripts
A script is basically a sequence of commands in a file. Bash reads the file and processes the commands in order. It moves on to the next command only when the current one has ended. (The exception being if a command has been specified to run asynchronously, in the background. Don't worry too much about the this case yet — we'll learn about how that works later on.)
Virtually any example that exists on the command line in this guide can also be used in a script.
Making a script is easy. Begin by making a new file, and put this on the first line:
#!/usr/bin/env bash
The header is called an interpreter directive (it is also called a hashbang or shebang). It makes sure that whenever our script is executed, Bash will be used as its interpreter. The way it works is this: when the kernel executes a non-binary file, it reads the first line of the file; if the line begins with #!, the kernel uses the line to determine the interpreter to relay the file to. (There are other valid ways to do this as well, see below.) The #! must be at the very start of the file, with no spaces or blank lines before it. Our script's commands will appear on separate lines below this.
Please do not be fooled by scripts or examples on the Internet that use /bin/sh as the interpreter. sh is not bash! Bash itself is a "sh-compatible" shell (meaning that it can run most 'sh' scripts and carries much of the same syntax) however, the opposite is not true; some features of Bash will break or cause unexpected behavior in sh.
Also, please refrain from giving scripts a .sh extension. It serves no purpose, and it's completely misleading (since it's going to be a bash script, not an sh script).
It is perfectly fine to use Windows to write scripts. Avoid, however, using Notepad. "Microsoft Notepad" can only make files with DOS-style line-endings. DOS-style line-endings end with two characters: a Carriage Return and a Newline character. Bash understands line-endings with only Newline characters. As a result, the Carriage Return character will cause an unexpected surprise if one doesn't know it's there (very weird error messages). If at all possible, use a more capable editor like Vim, Emacs, kate, GEdit... If one doesn't, the carriage returns will need to be removed from the scripts before running them.
Once the script file has been created, it can be executed by doing:
$ bash myscript
In this example, we execute bash and tell it to read the script. When we do it like this, the #! line is just a comment, Bash does not do anything at all with it.
Alternatively, we can give our scripts executable permissions. With this method, instead of calling Bash manually, we can execute the script as an application:
$ chmod +x myscript $ ./myscript
When executed in this way, the #! line tells the operating system (OS) what interpreter to use. The OS runs /usr/bin/env, which in turn runs bash, which reads our script.
To decide where to put the script, a couple alternatives exists: generally people like to keep their scripts in a personally-defined directory; a few, like to keep their scripts in an existent, executable PATH. To use a personal directory:
$ mkdir -p "$HOME/bin" $ echo 'PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc" $ source "$HOME/.bashrc"
The first command will make a directory called bin in the home, local, directory. It is traditional for directories that contain commands to be named bin, even when those commands are scripts and not compiled binary programs. The second command will add the directory's file path to the previous PATH variable and add that to the Bash configuration file (.bashrc). Every new instance of Bash will now check for executable scripts in defined directory. The third line reloads the Bash configuration file.
(Some people prefer to use a different directory to hold their personal scripts, such as $HOME/.config/bin or $HOME/.local/bin. You can use whatever you prefer, as long as you are consistent.)
Changes to Bash configuration file will not have an immediate effect, we have to make the step to re-read the file. In the example above, we used source "$HOME/.bashrc", we could have also used exec bash, or we could close the existing terminal and open a new one. Bash would then initialize itself again by reading .bashrc (and possibly other files).
In any case, we can now put our script in our bin directory and execute it as a normal command — we no longer need to prepend our script's name with the file path (which was the "./" part in the previous example):
$ mv myscript "$HOME/bin" $ myscript
Additionally:
Tip — If the system has multiple versions of bash installed, specifying the interpreter by absolute path may be desirable in order to ensure the right bash version is used. For example: #!/usr/bin/bash. Type "type -a bash" to print the paths to all Bash executables in PATH.
Tip — The interpreter can optionally be followed by one word of interpreter's options. For example, the following options will turn on verbose debugging: "#!/usr/bin/bash -xv". To learn more, see Debugging.
- Tip — After defining the interpreter in the header, it is recommended to summarize the scripts purpose. If desired, copyright information can be listed...: