This draft deletes the entire topic.
Examples
-
55Hello World
Interactive Shell
The Bash shell is commonly used interactively as a read-eval-print loop (REPL): It reads user commands, executes the commands, then prints the result. Many Unix-based and Unix-like operating systems use Bash as their default shell (notably Linux and macOS). The terminal automatically enters an interactive Bash shell process on startup.
Output
Hello World
by typing the following:echo "Hello World" #> Hello World # Output Example
Pro Tip: You can change the shell by just typing the name of the shell in terminal. For example:
sh
,bash
, etc...Note:
echo
is a Bash builtin command that writes the arguments it receives to the standard output. It appends a newline to the output, by default.
Non-Interactive Shell
The Bash shell can also be run non-interactively from a script, making the shell require no human interaction. Interactive behavior and scripted behavior should be identical - an important design consideration of Unix v7 Bourne shell and transitively Bash. Therefore anything that can be done at the command line can be put in a script file for reuse.
Follow these steps to create a
Hello World
script:-
Create a new file called
hello-world.sh
touch hello-world.sh
-
Make the script executable by running
chmod
+x hello-world.sh
-
Add this code:
#!/usr/bin/env bash echo "Hello World"
Line 1: The first line of the script must start with the character sequence
#!
, referred to as shebang1. The shebang instructs the operating system to run/bin/bash
, the Bash shell, passing it the script's path as an argument.E.g.
/bin/bash hello-world.sh
Line 2: Uses the
echo
command to writeHello World
to the standard output.
-
Execute the
hello-world.sh
script from the command line using one of the following:./hello-world.sh
- most commonly used, and recommended/bin/bash hello-world.sh
bash hello-world.sh
- assuming/bin
is in your$PATH
sh hello-world.sh
For real production use, you would omit the
.sh
extension (which is misleading anyway, since this is a Bash script, not ash
script) and perhaps move the file to a directory within yourPATH
so that it is available to you regardless of your current working directory, just like a system command such ascat
orls
.Common mistakes include:
-
Forgetting to apply execute permission on the file, i.e.
chmod +x hello-world.sh
, resulting in the output of./hello-world.sh: Permission denied
. -
Editing the script on Windows, which produces incorrect line ending characters that Bash cannot handle.
A common symptom is
: command not found
where the carriage return has forced the cursor to the beginning of line, overwriting the text before the colon in the error message.The script can be fixed using the
dos2unix
program.An example use:
dos2unix hello-world.sh
dos2unix
edits the file inline. -
Using
sh ./hello-world.sh
, not realizing thatbash
andsh
are distinct shells with distinct features (though since Bash is backwards-compatible, the opposite mistake is harmless).Anyway, simply relying on the script's shebang line is vastly preferable to explicitly writing
bash
orsh
(orpython
orperl
orawk
orruby
or...) before each script's file name.
1 Also referred to as sha-bang, hashbang, pound-bang, hash-pling.
-
-
Create a new file called
hello.sh
with the following content and give it executable permissions withchmod +x hello.sh
.Execute/Run via:
./hello.sh
#!/usr/bin/env bash # Note that spaces cannot be used around the `=` assignment operator whom_variable="World" # Use printf to safely output the data printf "Hello, %s\n" "$whom_variable" #> Hello, World
This will print
Hello, World
to standard output when executed.To tell bash where the script is you need to be very specific, by pointing it to the containing directory, normally with
./
if it is your working directory, where.
is an alias to the current directory. If you do not specify the directory,bash
tries to locate the script in one of the directories contained in the$PATH
environment variable.
The following code accepts an argument
$1
, which is the first command line argument, and outputs it in a formatted string, followingHello,
.Execute/Run via:
./hello.sh World
#!/usr/bin/env bash printf "Hello, %s\n" "$1" #> Hello, World
It is important to note that
$1
has to be quoted in double quote, not single quote."$1"
expands to the first command line argument, as desired, while'$1'
evaluates to literal string$1
.Security Note:
Read Security implications of forgetting to quote a variable in bash shells to understand the importance of placing the variable text within double quotes. -
-
The following will prompt a user for input, and then store that input as a string (text) in a variable. The variable is then used to give a message to the user.
#!/usr/bin/env bash echo "Who are you?" read name echo "Hello, $name."
The command
read
here reads one line of data from standard input into the variablename
. This is then referenced using$name
and printed to standard out usingecho
.Example output:
$ ./hello_world.sh Who are you? Matt Hello, Matt.
Here the user entered the name "Matt", and this code was used to say
Hello, Matt.
.
Versions
Version | Release Date |
---|---|
0.99 | 1989-06-08 |
1.01 | 1989-06-23 |
2.0 | 1996-12-31 |
2.02 | 1998-04-20 |
2.03 | 1999-02-19 |
2.04 | 2001-03-21 |
2.05b | 2002-07-17 |
3.0 | 2004-08-03 |
3.1 | 2005-12-08 |
3.2 | 2006-10-11 |
4.0 | 2009-02-20 |
4.1 | 2009-12-31 |
4.2 | 2011-02-13 |
4.3 | 2014-02-26 |
4.4 | 2016-09-15 |
Topic Outline
Versions
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft