Skip to main content
Provide explanation of steps
Source Link

Oneliner bash built-in (doesn't work for repetition):

DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 
  • Uses recursive shell parameter expansion to remove colon before or after removed directory
  • Pattern has two halves. One half is always empty, other provide reduced path.
  • Removes just one instance of $DIR from $PATH

Oneliner bash built-in (doesn't work for repetition):

DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 

Oneliner bash built-in:

DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 
  • Uses recursive shell parameter expansion to remove colon before or after removed directory
  • Pattern has two halves. One half is always empty, other provide reduced path.
  • Removes just one instance of $DIR from $PATH
Complete variable definition (no need to export DIR)
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

Oneliner bash built-in (doesn't work for repetition):

export DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 

Oneliner bash built-in (doesn't work for repetition):

export DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 

Oneliner bash built-in (doesn't work for repetition):

DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 

Oneliner bash built-in (doesn't work for repetition):

PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/}

export DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 

Oneliner bash built-in (doesn't work for repetition):

PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/}

Oneliner bash built-in (doesn't work for repetition):

export DIR=/dir/to/remove PATH=${PATH/${PATH/#$DIR:*/$DIR:}/}${PATH/${PATH/*:$DIR*/:$DIR}/} 
Source Link
Loading