Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

今まで ~/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

ironsand
思いついたアイデアをすぐに形にできる能力を身につけたいのでRailsを頑張って習得中。仕事ください。
http://programming.ironsand.net/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした