How to Use Variables and Comments in Shell Scripting

TL;DR
Comments in shell scripts start with a hash (#) and are ignored during execution, useful for documenting your code. Variables store data and come in two types: system variables (like $HOME, $PWD, $BASH) written in capitals, and user-defined variables. Reference any variable's value by placing a dollar sign before its name.
Transcript
Hello guys, welcome to the next video on shell scripting tutorial for beginners. In the last video, we have seen how we can create our first script and how we can execute our first hello world script. Now, in this video, we will talk about comments and variables. So, let's get started. So, first of all, let's talk about comments because they are re... Read More
Key Insights
- Comments are lines of code that are not executed by the script but provide helpful information about the script. You write them by placing a hash (#) at the start of a line, or after a line of code on the same line.
- Variables are containers that store data inside them. The stored data can be a string, a number, or any other kind of data that you want the script to hold and reuse.
- System variables are created and maintained by the Linux or Unix operating system. By standard convention they are defined in capital letters, so a variable written in uppercase is most likely a system variable.
- User-defined variables are created and maintained by the user. They are generally written in lowercase, but there is no strict rule, so you can also define them in capital letters if you prefer.
- You reference a variable's value by placing a dollar symbol ($) before the variable name. For example, echo $name prints the value stored in the name variable rather than the word 'name'.
- Common system variables include $BASH (the shell name), $BASH_VERSION (the bash version), $HOME (the home directory), and $PWD (the present working directory).
- You define a user-defined variable by writing a name, an equal sign, then the value, such as name=mark. You can mix variables with text inside echo, like echo "The name is $name".
- A variable name must not start with a number. If it does, it is not treated as a variable and the output is incorrect. Variable names should begin with a normal letter, either lowercase or uppercase.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: How do you write a comment in a shell script?
You write a comment by placing a hash (#) before a line of code, which turns that entire line into a comment that will not be executed. Alternatively, you can write a line of code and then add a hash after it, making everything following the hash a comment on that same line. These are the two most commonly used ways to add comments, though other methods exist. Comments are not executed but provide helpful information about your script.
Q: What is a variable in shell scripting?
A variable in shell scripting is a container that stores some data inside it. Whenever you define a variable, it holds some kind of data, which can be a string, a number, or any other kind of data. Variables let you store values and reuse them throughout your script. In shell, or any Unix system, there are two types of variables: system variables and user-defined variables, each maintained differently and following different naming conventions.
Q: What is the difference between system variables and user-defined variables?
System variables are created and maintained by your Linux or Unix operating system. They are predefined and, by standard convention, written in capital letters, so an uppercase variable is most likely a system variable. User-defined variables are created and maintained by the user, like us, and are generally written in lowercase. However, there is no strict rule requiring lowercase, so you can also write user-defined variables in capital letters if you want.
Q: How do you access the value of a variable in shell?
You access the value of a variable by placing a dollar symbol ($) before the variable name. For example, if you have a variable called name, you write echo $name to print its value. You can also combine variables with text, such as echo "The name is $name", which prints the surrounding text along with the variable's stored value. This dollar-sign syntax works for both system variables and user-defined variables inside your echo statements.
Q: How do you define a user-defined variable in a shell script?
You define a user-defined variable simply by giving it a name, adding an equal sign, then providing the value. For example, to create a variable called name with the value mark, you write name=mark. To use it, you place a dollar symbol before the name, like echo $name, which outputs mark. You can also store numbers, for example value=10, and print it with echo $value. Variables can be defined in lowercase or capital letters.
Q: What are some common system variables in shell?
Common system variables demonstrated include $BASH, which gives the name of the bash or shell; $BASH_VERSION, which gives the version of bash you are using; $HOME, which gives the home directory; and $PWD, which stands for present working directory and gives whatever directory you are currently working in, such as the desktop. These are accessed with a dollar symbol before the variable name and are predefined by the operating system. Many more system variables exist and can be found by searching.
Q: Why can't a variable name start with a number in shell?
A variable name should not start with a number because if it does, it will not be considered a variable. In the tutorial, defining a variable name beginning with the number 10 produced incorrect output, since the script disregarded it and did not treat the value as a variable. To fix this, you must start your variable name with a normal letter, either lowercase or capital, after which the variable prints its correct value. Variable names must begin with alphabetic characters.
Q: Can user-defined variables be written in capital letters?
Yes, user-defined variables can be written in capital letters. Although user-defined variables are generally defined in lowercase by convention, there is no strict rule requiring lowercase. The tutorial shows defining a variable in capital letters, such as naming it value in uppercase, and confirms it is totally fine and executes the same results. The lowercase convention mainly helps distinguish user-defined variables from system variables, which are conventionally written in capital letters, but you are free to use either case for your own variables.
Summary & Key Takeaways
-
Comments document a script without being executed. Write them by starting a line with a hash (#), or by adding a hash after a line of code. These are the two most commonly used ways, though other methods exist. Comments give information about the script but are ignored when it runs.
-
Variables are containers that store data such as strings or numbers. Shell has two types: system variables, created and maintained by the operating system and conventionally written in capitals, and user-defined variables, created by the user and generally written in lowercase, though capitals are also allowed.
-
Reference a variable using a dollar symbol before its name, for example echo $HOME prints the home directory. Define user variables as name=mark. A variable name cannot start with a number, or it will not be recognized; names must begin with a letter, lowercase or uppercase.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from ProgrammingKnowledge 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator