الدوال والوسائط في Shell Scripts
نظّم نصوصك البرمجية باستخدام دوال قابلة لإعادة الاستخدام، ومرّر الوسائط، وأعد القيم، وافهم نطاق المتغيرات في Bash.
الدوال والوسائط في Shell Scripts درس مجاني في Linux Command Line Mastery على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Linux Command Line Mastery، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Linux Command Line Mastery 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Functions?
You can write variables, conditionals, and loops, but as scripts grow they get repetitive. Functions let you name a block of logic and reuse it, keeping scripts clean and testable.
Defining a Function
A Bash function is a name followed by () and a body in braces. Define it before you call it.
greet() {
echo 'Hello from a function'
}
greetPassing Arguments
Functions receive arguments positionally: $1, $2, and so on. Just like the whole script does.
greet() {
echo "Hello, $1"
}
greet AliceAll Arguments at Once
$@ expands to all arguments and $# gives their count, handy for variadic helpers.
show() {
echo "count: $#"
for a in "$@"; do echo "arg: $a"; done
}
show one two threeReturn Status Codes
return sets an exit status (0-255), where 0 means success. Check it with $?.
is_even() {
if [ $(( $1 % 2 )) -eq 0 ]; then return 0; else return 1; fi
}
is_even 4 && echo evenReturning Real Values
Since return only gives a status, output data with echo and capture it via command substitution.
square() {
echo $(( $1 * $1 ))
}
result=$(square 5)
echo "result is $result"Local Variables
By default Bash variables are global. Use local inside functions to avoid clobbering outer variables.
calc() {
local total=0
for n in "$@"; do total=$(( total + n )); done
echo $total
}
calc 1 2 3 4Functions Calling Functions
Functions can call each other, letting you compose small pieces into larger behavior.
double() { echo $(( $1 * 2 )); }
quad() { double $(double $1); }
quad 3Default Argument Values
Use parameter expansion to supply a fallback when an argument is missing.
greet() {
local name=${1:-World}
echo "Hello, $name"
}
greetA Practical Helper
Functions shine for reusable utilities like logging with a timestamp prefix.
log() {
echo "[INFO] $*"
}
log 'Backup started'
log 'Backup finished'Organizing Scripts
A clean pattern: define all functions at the top, then a main function at the bottom that orchestrates them. This mirrors structured programming.
main() {
echo 'Running main'
}
main "$@"Quick Check
How do you reliably get a computed value back from a Bash function?
Recap
Functions make scripts modular:
- Define with
name() { ... } - Arguments are
$1,$@,$# return= status;echo+$()= valueslocalkeeps variables scoped
Compose small functions and orchestrate them from main.
الأسئلة الشائعة
هل درس «الدوال والوسائط في Shell Scripts» مجاني؟
نعم — نص درس «الدوال والوسائط في Shell Scripts» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Linux Command Line Mastery، انتقل إلى CoddyKit PRO. تتضمن دورة Linux Command Line Mastery 4 دروس في المجموع.
ماذا ستتعلم في «الدوال والوسائط في Shell Scripts»؟
نظّم نصوصك البرمجية باستخدام دوال قابلة لإعادة الاستخدام، ومرّر الوسائط، وأعد القيم، وافهم نطاق المتغيرات في Bash. تتمرن على Linux Command Line Mastery مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Linux Command Line Mastery؟
لا تُشترط خبرة سابقة. Linux Command Line Mastery على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الدوال والوسائط في Shell Scripts»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Linux Command Line Mastery هذا؟
نعم. كل درس في Linux Command Line Mastery يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- المتغيرات والإدخال والإخراج
- العبارات الشرطية: `if` و`else` و`case`
- الحلقات: `for` و`while` و`until`
- الدوال والوسائط في Shell Scripts