簡體   English   中英

sed:替換bash變量中的第n個字符

[英]sed: replace nth character from bash variable

我正在使用“類似於github”的路徑實用程序來更改目錄,但是我的sed無法替換唯一arg中的斜杠。

#!/bin/bash

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

運行此實用程序

customcmd site/project

應該回來

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

無需派生子外殼並使用sed 您正在使用bash因此請使用參數擴展

#!/bin/bash

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

在像customcmd ...這樣運行腳本之前,請確保已授予腳本可執行權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM