简体   繁体   中英

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.

#!/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 . You are using bash so use parameter expansion .

#!/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 ... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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