atomでパスをコピーした際にバックスラッシュを使ったC:\Users\みたいなパスがコピーされるので、ターミナルでディレクトリ移動する際にスラッシュのパスに置換したい。
つまりこうしたいC:\Users\→C:/Users/
コード
~/.bashrc
cd2(){
local fullPath=""
#「:」を「:/」にする
local x=${1/:/:/}
for (( i = 0; i < ${#1} + 2; i++ )); do
# 引数を先頭からfullPathに代入
fullPath+=${x:$i:1}
# ディレクトリが存在したら「/」を加える
if [[ -d $fullPath ]]; then
fullPath=$fullPath/
dirPath=$fullPath
fi
done
# 「//」を「/」に変換する、しかし「/」が複数付いてても平気みたい
# dirPath=${dirPath/\/\//\/};
cd "$dirPath"
}
こんな感じで使える
$ cd2 D:\documents\replace.sh
メモ
置換するだけかと思いきや「\」(バックスラッシュ)単体だと以下のように消えてしまう
$ echo D:\documents\replace.sh
D:documentsreplace.sh
なので-d フォルダパスでフォルダが存在するか確認して、ディレクトリを確認するごとに「/」(スラッシュ)を入れる
if [[ -d $fullPath ]]; then
fullPath=$fullPath/
dirPath=$fullPath
fi