linux $ 命令

终端命令中,一些符号拥有特异功能,使得我们可以简化很多操作,这些操作相当提高效率。

$!,这些符号与其他字母相组合,提供强大又方便的功能。

本文,看看$符号的用法

linux $x 说明

$x中x代表变量,可以是$、!、数字、-、+ 等等

$!$@$#$$$*$0$-$1~$n

注意顺序,看看键盘的符号顺序,会不会方便记忆

  • $!

shell最后运行的后台进程的pid

  • $@

所有参数列表。如 " <math xmlns="http://www.w3.org/1998/Math/MathML"> @ " 用「 " 」括起来的情况,以" @"用「"」括起来的情况,以" </math>@"用「"」括起来的情况,以"1" " <math xmlns="http://www.w3.org/1998/Math/MathML"> 2 " . . . " 2" ... " </math>2"..."n"的形成输出所有参数。注意与 $* 的区别

  • $#

添加到shell的参数个数

  • $$

shell本身的pid

  • $*

所有参数列表。如 " <math xmlns="http://www.w3.org/1998/Math/MathML"> @ " 用「 " 」括起来的情况,以" @"用「"」括起来的情况,以" </math>@"用「"」括起来的情况,以"1 <math xmlns="http://www.w3.org/1998/Math/MathML"> 2... 2 ... </math>2...n"的形成输出所有参数。注意与 $@ 的区别

  • $0

shell本身的文件名

  • $-

使用set命令设定的flag一览

  • $1~$n

添加到shell的各参数值。 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 是第一个参数、 1是第一个参数、 </math>1是第一个参数、2是第二个参数 ...。

一个文件说清楚用法

创建个shell文件:params.sh

bash 复制代码
#!/bin/bash
#
printf "The complete list $|! ==> shell最后运行的后台进程的pid  is %s\n" "$!"
printf "The complete list $|$ ==> shell本身的pid              is %s\n" "$$"
printf "The complete list $|? ==> 最后运行的命令的结束代码-返回值 is %s\n" "$?"

printf "The complete list $|0 ==> shell本身的文件名            is %s\n" "$0"

printf "The complete list $|# ==> 添加到shell的参数个数         is %s\n" "$#"
printf "The complete list $|1 ==> 添加到shell的第一个参数值      is %s\n" "$1"
printf "The complete list $|2 ==> 添加到shell的第二个参数值      is %s\n" "$2"
printf "The complete list $|@ ==> 所有参数列表,参数单独双引号    is %s\n" "$@"
printf "The complete list $|* ==> 所有参数列表,参数整体双引号    is %s\n" "$*"

执行文件

$ bash params.sh 123456 hello

结果:

dart 复制代码
The complete list $|! ==> shell最后运行的后台进程的pid  is
The complete list $|$ ==> shell本身的pid              is 30217
The complete list $|? ==> 最后运行的命令的结束代码-返回值 is 0
The complete list $|0 ==> shell本身的文件名            is params.sh
The complete list $|# ==> 添加到shell的参数个数         is 2
The complete list $|1 ==> 添加到shell的第一个参数值      is 123
The complete list $|2 ==> 添加到shell的第二个参数值      is hello
The complete list $|@ ==> 所有参数列表,参数单独双引号    is 123
The complete list $|@ ==> 所有参数列表,参数单独双引号    is hello
The complete list $|* ==> 所有参数列表,参数整体双引号    is 123 hello

$* "$*" $@ "$@" 的区别

NOTE: 我们传入相同方式的参数,观察四种写法的不同输出结果

bash 复制代码
echo "==> $|*"
for arg in $*; do
  echo "$arg"
done

echo "==> \"$|*\""
for arg in "$*"; do
  echo "$arg"
done

echo "==> $|@"
for arg in $@; do
  echo "$arg"
done

echo "==> \"$|@\""
for arg in "$@"; do
  echo "$arg"
done
  • 传入相同方式的参数,四种写法的不同输出

执行并传入参数

传入参数方式一 $ sh params.sh gu piao ji jin

ini 复制代码
==> $|*
gu
piao
ji
jin

==> "$|*"
gu piao ji jin

==> $|@
gu
piao
ji
jin

==> "$|@"
gu
piao
ji
jin

会发现结果没有差异

我们再执行并传入参数,注意参数的变化

传入参数方式二 sh params.sh "gu piao ji jin"
传入参数方式三 sh params.sh "gu" "piao" "ji" "jin"
传入参数方式四 sh params.sh "gu piao" "ji jin"

其他三种传入方式读者实践看看结果

我们给出结论

探讨了 $*$@其带引号的变体之间的相似点和不同点。它们在不带引号时的行为相同:它们将每个参数视为单独的实体,并保留各个参数中的空格和特殊字符。但是,如果我们将它们带引号,它们的行为会有所不同。虽然 "$@" 保留各个参数,但 "$*" 将它们视为单个实体。

在大多数情况下,使用 $@"$@"而不是 $* 在使用命令行参数时更安全、更灵活,因为它们保留了原始参数结构并使我们更容易使用 bash 脚本中的各个参数。

参考:<math xmlns="http://www.w3.org/1998/Math/MathML"> ∗ v s * vs </math>∗vs@

相关推荐
BestOrNothing_20153 小时前
(2)联想拯救者安装 Ubuntu 双系统前的 BIOS 设置全过程
linux·bios·拯救者·ubuntu22.04·联想lenovo
23.3 小时前
【Linux】grep命令终极指南
linux
巨斧空间掌门3 小时前
JDK17 下载 windows Linux
linux·运维·服务器
短剑重铸之日3 小时前
《ShardingSphere解读》07 读写分离:如何集成分库分表+数据库主从架构?
java·数据库·后端·架构·shardingsphere·分库分表
AI+程序员在路上4 小时前
CANopen 协议:介绍、调试命令与应用
linux·c语言·开发语言·网络
wefly20174 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
learndiary4 小时前
2026.03.12~2026.03.19制作的共7个视频及简介
linux·视频·学习日记小店
JiMoKuangXiangQu4 小时前
Linux:ARM64 中断处理简析
linux·arm64 中断
小生不才yz5 小时前
【Makefile 专家之路 | 函数篇】11. 终极奥义:eval 函数——动态生成规则的“核武器”
linux
皮卡蛋炒饭.5 小时前
进程得控制
linux·运维·服务器