bash - 从变量的末尾删除一个字符

标签 bash shell

Bash 自动补全会在目录名的末尾附加一个/。如何从位置参数中删除它?

#!/bin/sh

target=$1

function backup(){
  date=`date "+%y%m%d_%H%M%S"`
  PWD=`pwd`
  path=$PWD/$target
  tar czf /tmp/$date$target.tar.gz $path
}

backup

最佳答案

使用

target=${1%/}

A reference .

关于bash - 从变量的末尾删除一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1848415/

相关文章:

linux - 在 Bash 脚本中,如何读取文件并将所有行拆分为二维数组

linux - 在 suse linux 中远程启用和禁用 cronjob

bash - 在远程主机上下载大文件。然后关闭ssh连接

regex - sed 提取两个字符串之间的文本

Bash 脚本编号生成器

linux - 我可以在 shell 脚本中使用 hexdump 吗?

linux - shell 脚本 : how to group rows according to the first field/element in each row

bash - 在 heredoc 部分设置变量

python - 如何在 Wing IDE 中终止 Python shell?

shell - 重启后 chmod -> 权限更改