简体   繁体   English

sed:替换bash变量中的第n个字符

[英]sed: replace nth character from bash variable

I'm working on a "github-like" path utility to change directories, however my sed isn't working to replace the slash inside the only arg. 我正在使用“类似于github”的路径实用程序来更改目录,但是我的sed无法替换唯一arg中的斜杠。

#!/bin/bash

if [ -n "$1" ]; then
  echo "cd /path/to/folder/$1" | sed 's,/,/theme/,5'
fi

Running this utility 运行此实用程序

customcmd site/project

should return 应该回来

cd /path/to/folder/site/theme/project

No need to fork a sub-shell and use sed . 无需派生子外壳并使用sed You are using bash so use parameter expansion . 您正在使用bash因此请使用参数扩展

#!/bin/bash

if [ -n "$1" ]; then
  echo "cd /path/to/folder/${1/\//\/theme/}"
fi

Make sure you give the script executable permission before running it like customcmd ... . 在像customcmd ...这样运行脚本之前,请确保已授予脚本可执行权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM