Bash脚本自学 - 变量和位置自变量

变量

在hellothere.sh文件里,

#!/bin/bash

FIRST_NAME=Herbert
LAST_NAME=Lindemans
echo Hello $FIRST_NAME $LAST_NAME

在指令行输入:
chmod u+x hellothere.sh (授予这个文件所属者执行的权限)
./hellothere.sh
输出为:
Hello Herbert Lindemans

以下展示如何获取用户输入

在interactiveshell.sh文件中,

#!/bin/bash


echo What is your first name?
read FIRST_NAME
echo What is your last name?
read LAST_NAME

echo Hello $FIRST_NAME $LAST_NAME

在指令行输入:
chmod u+x interactiveshell.sh
./interactiveshell.sh
输出为:
What is your first name?
Jefferey
What is your last name?
Wu
Hello Jefferey Wu

位置自变量

在posargu.sh文件中,

#!/bin/bash

echo Hello $1 $2

在指令行输入:
chmod u+x posargu.sh
./posargu.sh Jefferey Wu

输出为:
Hello Jefferey Wu

你可能感兴趣的:(bash,bash,开发语言)