Sign Up

Continue with Facebook
Continue with Google
or use

Have an account? Sign In Now

Sign In

Continue with Facebook
Continue with Google
or use

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Continue with Facebook
Continue with Google
or use

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Raise Question

Raise Question Logo Raise Question Logo

Raise Question Navigation

  • Home
  • Blog
  • Contact Us
  • About Us
  • DMCA
  • Terms and Conditions
  • Privacy Policy
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add Post
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • Home
  • Blog
  • Contact Us
  • About Us
  • DMCA
  • Terms and Conditions
  • Privacy Policy
Home/ Questions/Q 570
Closed
Sachin Subedi
Sachin Subedi
Asked: January 2, 20222022-01-02T14:11:36+11:00 2022-01-02T14:11:36+11:00In: Programmers

How to declare variables in bash?

I want to declare a variable for a bash program but I don’t know about it so can anyone help me?

bashdeclaredeclare variablevariable
  • 5
  • 2 2 Answers
  • 45 Views
  • 0 Followers
  • 0
Share
  • Facebook

    Sorry this question is closed.

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. Best Answer
      Dhirendra Pant
      2022-01-03T21:13:04+11:00Added an answer on January 3, 2022 at 9:13 pm

      Bash scripts allows variable declaration as well as constant definition. There are two ways to declare a variable. The easiest is as follows:

      MyVariable=Content
      Notice that there is no space between the variable name, the equal sign and the content of the variable. When we use this method for declaring variables, the content of the variable can be a string or an integer. In other words, it is not a typed variable.

      When you want to refer to the content of a variable, like when you write it on the screen, you just have to put a dollar sign in front of the variable name.

      echo $MyVariable
      But if you want to do some operations other than displaying the value of a variable like performing a sum or concatenation you don’t have to use the dollar sign. You just have to use the variable names. If you find this confusing you can use the following rule of thumb. If you are writing a script and need to refer to the value of a variable and not to the variable itself just put a dollar sign in front of it.

      Take a look at the next example when I am creating a script with one string variable and one integer variable, then I echo both variables.

      #!/bin/bash

      MyVariable=”I will do some math!: ”
      Number=1

      echo $MyVariable $Number + $Number = $((Number + Number))
      The last line of the code may look complex, but it is not. Starting from left to right the “+” and the “=” signs are considered as text. The echo command echoes characters not variables. Remember the rule of thumb I told you? By putting the dollar sign I am referring to the text value of the variables. The other part of the statement, $((Number + Number)), pay close attention to the parenthesis. Parentheses are always evaluated from the inner to the outmost, a basic rule of algebra. The first parenthesis is (Number + Number), notice that Number is not preceded by a dollar sign. So according to the rule of thumb, we are referring to the variable Number and not to its content. Since the Number variable is numeric, we are doing a sum. Now the outer most parenthesis is preceded by a dollar sign so we are referring to the value of the expression (Number + Number).

      I created a graphic to show what that line does.

      This is an easy way to understand the code.
      On the next screen capture you will see the output of the previous script execution.

      This is the output of the sample script.

      • 3
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
      • Sachin Subedi
        2022-01-04T11:26:54+11:00Replied to answer on January 4, 2022 at 11:26 am

        Thanks, Bro

        • 0
        • Reply
        • Share
          Share
          • Share on Facebook
          • Share on Twitter
          • Share on LinkedIn
          • Share on WhatsApp

    Sidebar

    Ask A Question

    Stats

    • Questions 72
    • Answers 108
    • Best Answers 46
    • Users 29

    Adv 250×250

    aalan
    • Popular
    • Answers
    • Lokesh Giri

      How to withdraw tiktok balance?

      • 5 Answers
    • Suraj Pandey

      What is cpu ?

      • 4 Answers
    • Bigyan Darshan

      How to open facebook account?

      • 3 Answers
    • Rudra Sharma
      Rudra Sharma added an answer The Turing test is a measure of a machine's ability… January 21, 2023 at 6:09 am
    • Anonymous added an answer Artificial intelligence (AI) is the simulation of human intelligence in… January 14, 2023 at 8:24 pm
    • Anonymous added an answer There are several types of AI, including: Reactive machines: These… January 14, 2023 at 8:23 pm

    Top Members

    Laxmi Prasad Sapkota

    Laxmi Prasad Sapkota

    • 1 Question
    • 1,110 Points
    Enlightened
    Dhirendra Pant

    Dhirendra Pant

    • 6 Questions
    • 542 Points
    Professional
    Lokesh Giri

    Lokesh Giri

    • 6 Questions
    • 150 Points
    Pundit

    Adv 120×600

    Recent Posts

    • What is an Online Discussion Forum?
    • What is a Forum? | A blog post around the definition of a forum.
    • How to Stay Awake Until Midnight on New Year’s Eve | A blog post with tips and tricks on staying awake until midnight on New Year’s Eve.
    • How to Enjoy New Year’s Eve During the COVID 19 Pandemic | A blog post on how to have a healthy and safe New Year’s Eve.

    Recent Comments

    1. Rudra Sharma on What is the Turing test?
    2. Anonymous on What is artificial intelligence?
    3. Anonymous on What are the types of AI?
    4. Anonymous on What are some examples of AI in use today?
    5. Anonymous on What are the benefits of AI?

    Explore

    • Home
    • Add Post
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Raise Question

    Raise Question is a social questions & Answers Forum which will help you establis your community and connect with other people.

    About Us

    • Meet The Team
    • Blog
    • Contact Us

    Legal Stuff

    • Terms of Use
    • Privacy Policy
    • Cookie Policy

    Help

    • Knowledge Base
    • Support

    Best Hosting with 85% Black Friday Sale

    Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.

    © 2022 Raise Question. All Rights Reserved
    With Love by
    Webs Techie Pvt. Ltd..