• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Linux Tips / Understanding Variables in Shell Scripting

Understanding Variables in Shell Scripting

April 9, 2012 by cmdlinetips

A number of beautiful and powerful programming languages, like Python, Ruby, and Perl are available for scripting needs. Even then you may find the old school unix shell scripting very useful tool to learn and use. For sure, shell scripts looks crazy and hard to read/understand. Once you get a hang of it, you might find it extremely useful.

From the beginners’ perspective, one aspect of shell scripting that might drive one nuts is defining and using variables in a shell script. Here is the basics of understanding variables in shell scripting.

Defining a Variable in Shell Scripting

Just like any programming language, a variable is name for holding some value. Typically, shell variables hold string values. Variables in a shell script can begin with a letter or underscore and can contain letters/numbers/underscores. Here is example of defining a variable.

my_var=it_is_a_string

An important thing to note in defining a variable, there is no empty space between “=” sign and the variable name and value. And you may also have noticed that there is no “double quotes” in defining the string variable. The rule in shell scripting is that double quotes are not needed when there is no empty space in the variable. And you do need double quotes, if the value of your variable has empty space in it, like

my_var=”it is a string”.

How to Use a Variable in Shell Scripting?

IN shell scripting the variable name can not be simply used to get the value of variable.
To use the variable, one need to use “$” sign in front of the variable name, like

echo $my_var

will print the value of “my_var” on the terminal. (The use of the variable like “echo my_var” will result in error”.)

Just like other languages, one variable can be assigned to another variable, like

my_new_var = $my_var

. However, if one uses more than one variable in defining a new variable, they have to be with a double quote. For example, we have two variables

last_name=Smith
first_name=John

and we want to create a new variable “full_name” using the above two variables, then

full_name = “$first_name $last_name”.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Check If a File or Directory Exists Using Shell Script? How Set PATH Variable on BASH Shell: Hint export commandHow To Set PATH variable on Bash Shell? Default ThumbnailHow To Loop Through Lines in a File in Bash? Default ThumbnailHow to Run a Shell Command from Python and Get The Output?

Filed Under: Linux Tips, Shell Scripting, Uncategorized Tagged With: Linux Tips, Shell Scripting, Variables in Shell Scripts

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version