簡體   English   中英

shell 別名在主目錄中執行

[英]shell alias executes in home directory

我的.zshrc文件中有以下行:

alias clean="sed -i 's/\r//g; s/    /\t/g' $(find . -maxdepth 1 -type f)"

但是當我嘗試在/path/to/some/directory中執行它時,output 是:

sed ./.Xauthority
./.lesshst: No such file or directory

.Xauthority.lesshst都在我的主目錄中。

替換. 使用$(pwd)沒有幫助。

在定義別名時,您使用了雙引號來包含整個(別名)定義。 這具有在定義別名時實際運行find命令的效果。

因此,當創建別名時,它將從定義別名的目錄中獲取文件列表(例如,在獲取.zshrc時在您的主目錄中)。

您可以在以下示例中看到這種情況:

$ cd /tmp

$ pwd
/tmp

$ ls -l
total 36036
drwxrwxrwt+ 1 myid None                  0 Oct 10 11:31 ./
drwxr-xr-x+ 1 myid None                  0 Jul 12 17:28 ../
-rw-r--r--  1 myid Administrators        0 Oct 10 11:31 a
-rw-r--r--  1 myid Administrators        0 Oct 10 11:31 b
-rw-r--r--  1 myid Administrators        0 Oct 10 11:31 c
-rw-r--r--  1 myid Administrators        0 Oct 10 11:31 d
-rw-r--r--  1 myid Administrators 36864002 Jun  6 17:29 giga.txt
drwx------+ 1 myid Administrators        0 Mar  8  2020 runtime-xward/

$ alias clean="sed -i 's/\r//g; s/    /\t/g' $(find . -maxdepth 1 -type f)"
$ alias clean
alias clean='sed -i '\''s/\r//g; s/    /\t/g'\'' ./a
./b
./c
./d
./giga.txt'

請注意如何在別名定義時評估find並提取我的/tmp目錄中的所有文件。

要解決此問題,您需要確保在創建別名時評估find

有幾種方法可以做到這一點,一個想法是將定義的find部分用單引號引起來,另一個想法是保留當前的雙引號並轉義$ ,例如:

$ alias clean="sed -i 's/\r//g; s/    /\t/g' "'$(find . -maxdepth 1 -type f)'
$ alias clean
alias clean='sed -i '\''s/\r//g; s/    /\t/g'\'' $(find . -maxdepth 1 -type f)'

$ alias alias clean="sed -i 's/\r//g; s/    /\t/g' \$(find . -maxdepth 1 -type f)"
$ alias clean
alias clean='sed -i '\''s/\r//g; s/    /\t/g'\'' $(find . -maxdepth 1 -type f)'

請注意,在這兩種情況下,別名都包含實際的find命令,而不是在當前目錄中對其進行評估的結果。

暫無
暫無

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

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