• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Bash : print variable value inside single and double quotes

October 16, 2014 by Sharad Chhetri Leave a Comment

In this post we will learn, how to print variable value inside single and double quotes. Today I was working on some bash scripting and got this requirement. The script is working wonderfully and as expecting output was also coming. Today we will share this basic bash scripting.

Explaining step by step , which gives you more understanding

Variable = foo
Value = bar

Define Variable and its value

In below example, we have defined the variable foo with value bar

[root@localhost ~]# foo=bar
[root@localhost ~]#

Print Variable’s value

Print variable’s value by using $ inside double quotes.

[root@localhost ~]# echo "$foo"
bar
[root@localhost ~]#

Or using $ sign only to print Variable’s value

[root@localhost ~]# echo $foo
bar
[root@localhost ~]#

What happen when we try to call variable by using single quotes. See the below example.
In this example, you can see that by using single quotes it is printing $ sign with variable name. Means not calling the variables value even by using $ sign.

[root@localhost ~]# echo '$foo'
$foo
[root@localhost ~]#

In above lesson we learn that when we use $ sign with variable inside double quotes, it will print the variable’s value.

Print Variable’s value inside single quotes

In this example, inside double quotes we have written variable with $ sign again inside the single quotes.
Then it prints the variable’s value inside single quotes.

[root@localhost ~]# echo "'$foo'"
'bar'
[root@localhost ~]#

Print Variable’s value inside double quotes

Now our requirement is to print variable’s value inside double quotes.
See the below given example

[root@localhost ~]# echo ""$foo""
"bar"
[root@localhost ~]# 

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Print double hyphen sign simultaneously in post of Octopress
  2. Read the file inside compressed .gz files without extract
  3. How to create a search box inside page in WordPress
  4. 500 OOPS: vsftpd: refusing to run with writable root inside chroot()
  5. postgres database backup script using database user password inside
  6. print working directory ( pwd , PWD , OLDPWD ) in Linux / Unix
  7. awk command to print columns from file
  8. How to print particular line number by using sed command
  9. Print grep command output without seperator
  10. print new line character in Unix / Linux

Filed Under: Linux Tagged With: bash script

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

Useful tail command with examples in Linux/Unix

Linux command : To create new directory/folder

Install libjpegtran by using yum command in CentOS 6.x

How to make rm -fr command to be interactive

Command to create and delete group in Red Hat and CentOS linux

Give permission reference to file/dir from other file/dir in linux

FAQ on CentOS 7 / RHEL 7 : Alternate of ifconfig command

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

How to forcefully send mail from mailq in sendmail MTA

Password prompt in single user mode is not secure : CentOS/Red Hat

How to create a file with cat command

linux release renew dhcp assigned ip address

How to set JAVA environment variables in Linux or CentOS

Terminal Recording with script and scriptreplay command

How to find when Operating system was installed in linux CentOS and Red Hat

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy