Skip to main content

BASH | VARIABLES | LINUX SERIES


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 .

Hello World with User Input

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 variable name . This is then referenced using $name and printed to standard out using echo .

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. .

And if you want to append something to the variable value while printing it, use curly brackets around the variable name as shown in the following example:

#!/usr/bin/env bash
echo "What are you doing?"
read action
echo "You are ${action}ing."

Example output:

$ ./hello_world.sh
What are you doing?
Sleep
You are Sleeping.

Here when user enters an action, "ing" is appended to that action while printing.

Importance of Quoting in Strings

Quoting is important for string expansion in bash. With these, you can control how the bash parses and expands your strings.

There are two types of quoting:
1. Weak: uses double quotes: "
2. Strong: uses single quotes: '

If you want to bash to expand your argument, you can use Weak Quoting:

#!/usr/bin/env bash
world="World"
echo "Hello $world"
#> Hello World

If you don't want to bash to expand your argument, you can use Strong Quoting:

#!/usr/bin/env bash
world="World"
echo 'Hello $world'
#> Hello $world

You can also use escape to prevent expansion:

#!/usr/bin/env bash
world="World"
echo "Hello \$world"
#> Hello $world


Peace Out.
@suhaibbinyounis


Comments

Popular posts from this blog

Make money with Quora Partner Program | Explained

Earning money ain't it...
I know we all have some time wondered if it is possible to earn money online and believe me there are many ways to do that and it is all amazing.
In this post I will specifically talk about Quora Partner Program, QPP.

Quora came out with this amazing program that allows anyone in world to make a living out of writing. But speaking the truth it is not that much. It quite a hard work and less pay job.
Bascially, all you do is ask questions and then these questions will receive Ad impressions for which you get paid.

But here is the deal, you can monetize a question only for 1 year only. After that Quora owns the question.


So, how do you get started?

As of now there is no direct way of joining the QPP. The only way for you to join is if you receive a email from Quora itself which is quite random and obviously how active you are on quora will play a part.


So, how much can i earn?
Well, there is no limit on how much you can earn. Generally the money is way too low but t…

RESOLVING NETWORK ISSUES ON LINUX | LINUX SERIES

Dealing with networking issues can be a very challenging task. A packet sent from a client to a server (or vice versa) might be dropped in various locations and for various reasons. Some of the most common cases are -
1. A firewall that is blocking the packet. It can be a firewall on the server side, on the client side or any firewall along the way between the server and the client. The blocking may be based on an ip (source and/or destination), ports (in case of tcp/udp) or any other layer 3,4 criteria.

2. Lack of layer 2 connectivity.

3. Routing configurations that are not set properly.

Fortunately, Linux command line tools supply significant information that might help us narrow down the problem. From my experience those are my top 8 tools that will help you overcome networking issues.
 ping
This is the most fundamental command when troubleshooting a networking issue. The ping command will test the network connectivity between your host and the server. This is definitely the first comman…

Magic SysRq key | Linux Rescue | Frozen Linux | LINUX SERIES

The magic SysRq key is a key combination which allows the user to perform various low-level commands regardless of the system's state. If your Linux machine freezes or you want to reboot your Linux machine without corrupting the filesystem you can use this magic SysRq key combination. This key combination provides access to powerful features and thus needs some knowledge to use it. Please do not proceed without knowing about these combinations as you may put your system in a critical state. Please note that this magic SysRq key cannot work under certain conditions, such as a kernel panic or a hardware failure preventing the kernel from running properly.
Commads:The key combination consists of Alt+SysRq and another key, which controls the command issued. SysRq may be released before pressing the command key, as long as Alt remains held down.
These combinations always assume the QWERTY keyboard layout.
1. Immediately reboot the system, without unmounting or syncing filesystems - b 2. Perfor…