我很難得到這個bash腳本來執行格式化的輸入。 它非常直接,但是當它執行以'newstring ='開頭的行時,它不執行sed操作,它只打印我的輸入(直到第一個空格),然後直接打印我的sed命令後。我究竟做錯了什麼?簡單的bash腳本給我的問題
#! /bin/bash
##format paths/strings with spaces to escape the spaces with a forward-slash'\'
##then use 'open' to open finder at current-set directory (based on path)
oldstring="$1"
newstring="$oldstring | sed 's/ /\\ /g')"
cd $newstring
open .
但是,使用'newstring = $(echo「$ oldstring」| sed's// \\/g')'好多了。 –
另外,在'bash'(但不是其他的shell AFAIK)中,你可以使用:'newstring = $(sed's// \\/g'<<<「$ oldstring」)''。 '$ oldstring'周圍的雙引號可能不是必需的,但是不會造成任何傷害。 –
並且,在陳述結尾處刪除懸掛''' –