linux - 如何获取shell命令的位置参数
问题描述
示例 Bash 脚本:
#!/bin/bash
echo $0
echo $1
运行这个
$ ./prog foo
会打印:
./prog
foo
所以我知道如何在 shell 程序中获取位置参数。
我想弄清楚的是如何对任何其他 shell 命令执行相同的操作。比如mv
,我想说
mv file1 .file1.bak
但我不想两次都输入 file1 。
我尝试了与上面的 shell 脚本相同的方法。
$ echo a b c $0 $1 $2
但这印
a b c -zsh
我认为它可能只是一个特定于 shell 的东西,所以我在bash
.
a b c bash
TL;DR:如何从扩展中的 shell 命令中获取位置参数?
解决方案
您可以使用以下方法之一:
!^ first argument
!$ last argument
!* all arguments
!:2 second argument
!:2-3 second to third arguments
!:2-$ second to last arguments
!:2* second to last arguments
!:2- second to next to last arguments
!:0 the command
!! repeat the previous line
前任:
echo a b c d e
> a b c d e
echo !$
> echo e
> e
echo "a" "b" "c" "d" "e"
> a b c d e
echo "!:2"
> echo ""b""
> b
这些是操纵bash 历史的命令。
推荐阅读
- sql - H2 不允许我批量插入数据
- mysql - SQL查询,获取所有没有给定状态值的id
- discord - 我正在尝试制作一个不和谐的机器人,让 DM 是随机用户,它要么只有我,要么我得到一个错误
- ruby-on-rails - Rails Devise,如何正确嵌套资源
- excel - Excel VBA 的局限性?
- python - 使用python,我如何找出一个字符串是否包含除另一个列表中的字符之外的任何字符
- r - R If then do - 如何根据条件创建变量
- arrays - 将 PowerShell 中的多值属性导出到 Excel - 尝试使用哈希表数组实现自动化
- asp.net-core - 基于约定的路由的路由名称
- node.js - Koa-Router:如果请求不在 XHR 中,则跳过路由