I want to display it at the prompt with ~ / f / bar
like fish
string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp
It has become.
Since string replace
is a function of fish, it is reproduced with bash.
function abbr_pwd() {
local p=$(echo $PWD | sed "s@$HOME@~@")
local directories=(${p//\// })
local _n_d=${#directories[@]}
local _last=${directories[_n_d - 1]}
local _abbr_pwd=""
if [ ${directories[0]} != "~" ]; then
_abbr_pwd=$_abbr_pwd/
fi
#Actually if[[ $p =~ (.?[^\/]{1})[^\/]*\/ ]]I want to process with
for d in "${directories[@]:0:_n_d-1}"; do
_abbr_pwd=$_abbr_pwd${d:0:1}/
done
echo $_abbr_pwd$_last
}
I want to process with a regular expression like the code of fish, but if [[$ p = ~ (.? [^ \ /] {1}) [^ \ /] * \ /]]; then; echo $ { BASH_REMATCH [@]}; fi
only got the first match ~
. (If you look it up, you can do it)
Replace the directory path / home / username / foo / bar
with sed
to make it ~ / foo / bar
I decomposed the character string with " / "
, combined with "/" leaving only the first character except the last part, and added bar at the end to get ~ / f / bar
.
In the root path from $ HOME
,/
is put at the beginning like / usr / share / theme
-> / u / s / theme
.
However, if it is under a home directory other than your own user (cannot be replaced with $ HOME), it becomes redundant like / h / o (otherUser) / f / bar
.
PS1='[\u@\h] $(abbr_pwd $PWD) \$ '
Please play with it as appropriate.