← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 11 of 12

Variables का परिचय

एक variable एक named box है जो text या number का एक टुकड़ा store करता है ताकि आपकी script बाद में इसे नाम से refer कर सके।
Syntax
bash
variable_name=value
echo "$variable_name"
echo "${variable_name}suffix"

एक Variable Assign और पढ़ना

आप एक variable name=value से assign करते हैं, = के आसपास कोई spaces नहीं। Value वापस पढ़ने के लिए आप नाम के आगे एक dollar sign लगाते हैं, $name, जिसे Bash command run करने से पहले stored text के साथ expand (substitute) करता है।

Warning: name = "Ada" (spaces के साथ) fail होता है क्योंकि Bash name नाम का एक command run करने की कोशिश करता है, arguments = और "Ada" के साथ।

उदाहरण: Assigning and Reading a Variable

bash
#!/bin/bash
name="Ada Lovelace"
echo "Hello, $name!"

Braces उपयोग करना: ${name}

एक variable name को braces में wrap करना, ${name}, variable name की boundary explicit बनाता है, जो मायने रखता है जब आप इसके immediately बाद बिना किसी separator के दूसरा text लगाना चाहें।

Braces के बिना, Bash उस बाद वाले text को variable name का हिस्सा मानने की कोशिश करेगा।

उदाहरण: Using Braces: ${name}

bash
#!/bin/bash
fruit="apple"
echo "I have one ${fruit}s"
# echo "I have one $fruits" would look for a variable named 'fruits' instead

Variables को Reassign और उन पर Build करना

Variables को script में कभी भी reassign किया जा सकता है, और एक variable की current value इसकी नई value बनाते समय उपयोग की जा सकती है। यह कई steps में strings या counters build up करने के लिए आम है।

उदाहरण: Reassigning and Building on Variables

bash
#!/bin/bash
greeting="Hello"
greeting="$greeting, world"
echo "$greeting"
count=1
count=$((count + 1))
echo "Count is now $count"

Default रूप से सब कुछ एक String है

Bash के पास out of the box distinct numeric या boolean variable types नहीं हैं -- हर variable text रखता है, और Bash उस text को सिर्फ arithmetic expressions जैसे numeric contexts में एक number के रूप में reinterpret करता है।

यही कारण है कि age="5" और age=5 store होने पर identically behave करते हैं।

उदाहरण: Everything Is a String by Default

bash
#!/bin/bash
age="5"
echo "Stored value: $age"
echo "Type as text length: ${#age} character(s)"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. = sign के आसपास spaces लगाना, जैसे name = "Ada"; Bash को कोई spaces नहीं चाहिए (name="Ada"), otherwise यह name को एक command के रूप में run करने की कोशिश करता है।
  2. एक variable की value पढ़ते समय $ भूल जाना; name कुछ नहीं/एक command को refer करता है, जबकि $name stored value retrieve करता है।
  3. यह उम्मीद करना कि variables दूसरी languages की तरह typed हैं; default रूप से हर Bash variable एक string के रूप में stored है, भले ही यह एक number जैसा दिखे।
चैप्टर सारांश
  • name=value से assign करें (= के आसपास कोई spaces नहीं); $name या ${name} से पढ़ें।
  • Variable names case-sensitive हैं और conventionally script-local variables के लिए lowercase, exported/environment variables के लिए uppercase उपयोग करते हैं।
  • ${name} (braces के साथ) $name से safer है जब variable name के immediately बाद दूसरे characters आएं, जैसे ${name}s।
  • Unset variables default रूप से एक empty string में expand होते हैं, एक error cause करने के बजाय (जब तक set -u उपयोग न हो)।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.