Getting started
Using trap to react to signals and system events
Listing files
Aliasing
Jobs and processes
Redirection
Control structures
Using cat
Arrays
Functions
Bash Parameter Expansion
Sourcing
Find
Here documents and here strings
Quoting
Conditional Expressions
Scripting with parameters
History substitutions
Math
Scoping
Process substitution
Programmable completion
Customizing PS1
Brace Expansion
Bash Arithmetic
getopts smart positional-parameter parsing
Debugging
Pitfalls
Script shebang
Pattern matching and regular expressions
Keyboard shortcuts
Change shell
Copying with cp
Internal variables
Job control
Case statement
Word splitting
Read a file
File transfer with scp
Pipelines
Managing PATH environment variable
Avoiding date using printf
Chain of commands and operations
Type of shells
true, false and commands
Color script output cross-platform
Navigating directories
Using sort
Namespace
co-processes
Typing variables
Jobs at specific types
Associative arrays
Handling the system prompt
Creating directories
File execution sequence
The cut command
Bash on Windows 10
Cut command
Splitting files
Global and local variables
Design Patterns
CGI Scripts
Select keyword
When to use eval
Networking with bash
Parallel
Grep
Strace
Sleep
Decodi
Contributors

Hello World Using Variables

suggest change

Create a new file called hello.sh with the following content and give it executable permissions with chmod +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, following Hello,.

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents