Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 90821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:56:00+00:00 2026-05-10T22:56:00+00:00

Or more generally, how do I remove an item from a colon-separated list in

  • 0

Or more generally, how do I remove an item from a colon-separated list in a Bash environment variable?

I thought I had seen a simple way to do this years ago, using the more advanced forms of Bash variable expansion, but if so I’ve lost track of it. A quick search of Google turned up surprisingly few relevant results and none that I would call ‘simple’ or ‘elegant’. For example, two methods using sed and awk, respectively:

PATH=$(echo $PATH | sed -e 's;:\?/home/user/bin;;' -e 's;/home/user/bin:\?;;') PATH=!(awk -F: '{for(i=1;i<=NF;i++){if(!($i in a)){a[$i];printf s$i;s=':'}}}'<<<$PATH) 

Does nothing straightforward exist? Is there anything analogous to a split() function in Bash?

Update:
It looks like I need to apologize for my intentionally-vague question; I was less interested in solving a specific use-case than in provoking good discussion. Fortunately, I got it!

There are some very clever techniques here. In the end, I’ve added the following three functions to my toolbox. The magic happens in path_remove, which is based largely on Martin York’s clever use of awk‘s RS variable.

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/:$//'`; } 

The only real cruft in there is the use of sed to remove the trailing colon. Considering how straightforward the rest of Martin’s solution is, though, I’m quite willing to live with it!


Related question: How do I manipulate $PATH elements in shell scripts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:56:01+00:00Added an answer on May 10, 2026 at 10:56 pm

    A minute with awk:

    # Strip all paths with SDE in them. # export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'` 

    Edit: It response to comments below:

    $ export a='/a/b/c/d/e:/a/b/c/d/g/k/i:/a/b/c/d/f:/a/b/c/g:/a/b/c/d/g/i' $ echo ${a} /a/b/c/d/e:/a/b/c/d/f:/a/b/c/g:/a/b/c/d/g/i  ## Remove multiple (any directory with a: all of them) $ echo ${a} | awk -v RS=: -v ORS=: '/a/ {next} {print}' ## Works fine all removed  ## Remove multiple including last two: (any directory with g) $ echo ${a} | awk -v RS=: -v ORS=: '/g/ {next} {print}' /a/b/c/d/e:/a/b/c/d/f: ## Works fine: Again! 

    Edit in response to security problem: (that is not relevant to the question)

    export PATH=$(echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}' | sed 's/:*$//') 

    This removes any trailing colons left by deleting the last entries, which would effectively add . to your path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

More generally, given a procedure that has both a synchronous and asynchronous version (like
I know this has been asked more generally before, but here is my specific
Does anyone know in which programming language the Googlebot was written? Or, more generally,
I've been lately trying to learn more and generally test Java's serialization for both
I know that exceptions have a performance penalty, and that it's generally more efficient
I have a method that takes a condition and item, from an array of
I try to remove some XmlElements from my Xml file in C#2.0. I can
As I understand it (not that I'm correct) Drawables are generally correctly removed from
this may be a more general opengl question. using OpenGL ES for 2d, and
I'm also interested in more general thoughts, but here is my specific problem. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.