LoginSignup
85
84

More than 5 years have passed since last update.

$PATH から特定のパスだけを削除する

Posted at

今まで ~/sh に自分で書いたシェルスクリプトを入れてたんだけど、 ~/bin に入れといたほうがlinuxっぽい気がするので ~/sh/foo.bash を作っといて ~/sh/foo にリンクを置くことにしました。

それで環境変数PATHに入ってる ~/sh を消したいので ~/.profile を編集して $ . .profile すりゃ消えるだろうと思ってたら消えない!

消し方

調べてみたら結構めんどくさそうだったけど、頭の良い人が既に解決策として関数を用意してくれてたのでそれを使うことにしました。自分の頭を使わなくて済むって素晴らしい。ヽ(´ー`)ノ

~/.bashrc
path_append ()  { path_remove $1; export PATH="$PATH:$1"; }
path_prepend () { path_remove $1; export PATH="$1:$PATH"; }
path_remove ()  { export PATH=`echo -n $PATH | awk -v RS=: -v ORS=: '$0 != "'$1'"' | sed 's/:$//'`; }

で関数を追加して

$ . ~/.bashrc
$ path_remove ~/sh

~/sh を環境変数から消せました。

所でQiitaで表示に使える言語に shell-session なんてのもあったんですね。今回はじめて使いました。

参考

What is the most elegant way to remove a path from the $PATH variable in Bash? - Stack Overflow
http://stackoverflow.com/questions/370047/what-is-the-most-elegant-way-to-remove-a-path-from-the-path-variable-in-bash

85
84
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
85
84