Variables का परिचय
In this page:
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) करता है।
name = "Ada" (spaces के साथ) fail होता है क्योंकि Bash name नाम का एक command run करने की कोशिश करता है, arguments = और "Ada" के साथ।उदाहरण: Assigning and Reading a Variable
#!/bin/bash
name="Ada Lovelace"
echo "Hello, $name!"
Login to try C/C++/Java/PHP code in the editor
Braces उपयोग करना: ${name}
एक variable name को braces में wrap करना, ${name}, variable name की boundary explicit बनाता है, जो मायने रखता है जब आप इसके immediately बाद बिना किसी separator के दूसरा text लगाना चाहें।
Braces के बिना, Bash उस बाद वाले text को variable name का हिस्सा मानने की कोशिश करेगा।
उदाहरण: Using Braces: ${name}
#!/bin/bash
fruit="apple"
echo "I have one ${fruit}s"
# echo "I have one $fruits" would look for a variable named 'fruits' instead
Login to try C/C++/Java/PHP code in the editor
Variables को Reassign और उन पर Build करना
Variables को script में कभी भी reassign किया जा सकता है, और एक variable की current value इसकी नई value बनाते समय उपयोग की जा सकती है। यह कई steps में strings या counters build up करने के लिए आम है।
उदाहरण: Reassigning and Building on Variables
#!/bin/bash
greeting="Hello"
greeting="$greeting, world"
echo "$greeting"
count=1
count=$((count + 1))
echo "Count is now $count"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
age="5"
echo "Stored value: $age"
echo "Type as text length: ${#age} character(s)"
Login to try C/C++/Java/PHP code in the editor
=sign के आसपास spaces लगाना, जैसेname = "Ada"; Bash को कोई spaces नहीं चाहिए (name="Ada"), otherwise यहnameको एक command के रूप में run करने की कोशिश करता है।- एक variable की value पढ़ते समय
$भूल जाना;nameकुछ नहीं/एक command को refer करता है, जबकि$namestored value retrieve करता है। - यह उम्मीद करना कि 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उपयोग न हो)।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: