i4n*_*i4n 0 macos bash terminal formatting
我很难获得这个bash脚本来执行输入的格式化.这很简单,但是当它执行以'newstring ='开头的行时,它不执行sed操作,它只打印我的输入(直到第一个空格)然后直接打印我的sed命令.我究竟做错了什么?
#! /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 .
Run Code Online (Sandbox Code Playgroud)
你应该这样做:
cd "$1"
open .
Run Code Online (Sandbox Code Playgroud)
这样可以避免运行子进程并处理sed脚本没有的各种问题(例如包含$符号的名称或其他shell元字符).通常,如果变量(或位置参数,例如$1)是可以包含空格的文件名,则每次都使用双引号括起来.
| 归档时间: |
|
| 查看次数: |
94 次 |
| 最近记录: |