{"id":1441,"date":"2016-06-22T17:15:56","date_gmt":"2016-06-22T14:15:56","guid":{"rendered":"http:\/\/www.systemcodegeeks.com\/?p=1441"},"modified":"2016-06-20T19:29:42","modified_gmt":"2016-06-20T16:29:42","slug":"unix-split-string-using-separator","status":"publish","type":"post","link":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/","title":{"rendered":"Unix: Split string using separator"},"content":{"rendered":"<p>I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019.<\/p>\n<p>e.g. an example of one of the strings<\/p>\n<pre class=\"brush:bash\">A\/B\/C<\/pre>\n<p>I wanted to write some code that could split on \u2018\/\u2019 and then pick the 3rd item in the resulting collection.<\/p>\n<p>One way of doing this is to echo the string and then pipe it through cut:<\/p>\n<pre class=\"brush:java\">$ string=\"A\/B\/C\"\r\n$ echo ${string} | cut -d\"\/\" -f3\r\nC<\/pre>\n<p>or awk:<\/p>\n<pre class=\"brush:java\">$ echo ${string} | awk -F\"\/\" '{ print $3}'\r\nC<\/pre>\n<p>I don\u2019t like having to echo the string \u2013 it feels a bit odd so I wanted to see if there was a way to do the parsing more \u2018inline\u2019.<\/p>\n<p>I came across <a href=\"http:\/\/stackoverflow.com\/questions\/918886\/how-do-i-split-a-string-on-a-delimiter-in-bash\">this post<\/a> which explains how to change the internal field separator (IFS) on the shell and then parse the string into an array using <a href=\"http:\/\/ss64.com\/bash\/read.html\">read<\/a>. I gave it a try:<\/p>\n<pre class=\"brush:bash\">$ IFS=\"\/\" read -ra ADDR &lt;&lt;&lt; \"${string}\"; echo ${ADDR[2]}\r\nC<\/pre>\n<p>Works! We can even refer to the last item in the array using -1 instead of it\u2019s absolute position:<\/p>\n<pre class=\"brush:bash\">$ IFS=\"\/\" read -ra ADDR &lt;&lt;&lt; \"${string}\"; echo ${ADDR[-1]}\r\nC<\/pre>\n<p>I\u2019d not come across this use of the \u2018read\u2019 function before. The key is the \u2018-a\u2019 parameter. From the man page:<\/p>\n<blockquote><p>-a aname<br \/>\nThe words are assigned to sequential indices of the array variable aname,<br \/>\nstarting at 0. All elements are removed from aname before the assignment.<br \/>\nOther name arguments are ignored.<\/p><\/blockquote>\n<p>So we\u2019re resetting the internal field separator and then reading the string into another variable as an array split on the \u2018\/\u2019 character.<\/p>\n<p>Pretty neat although now it\u2019s longer than the original command and I\u2019m sure I\u2019ll forget the syntax.<\/p>\n<p>Further down the page is another suggestion which seems even harder to remember but is much shorter:<\/p>\n<pre class=\"brush:bash\">$ echo ${string##*\/} \r\nC<\/pre>\n<p>This drops from the beginning of the string up until the last occurrence of \u2018\/\u2019 which is exactly what we want.<\/p>\n<p>This way is the nicest and doesn\u2019t require any echoing if we just want to assign the result to a variable. The echo is only used here to see the output.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.markhneedham.com\/blog\/2016\/06\/19\/unix-split-string-using-separator\/\">Unix: Split string using separator<\/a> from our <a href=\"http:\/\/www.systemcodegeeks.com\/join-us\/scg\/\">SCG partner<\/a>\u00a0Mark Needham at the <a href=\"http:\/\/www.markhneedham.com\/blog\/\">Mark Needham Blog<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an example of one of the strings A\/B\/C I wanted to write some code that could split on \u2018\/\u2019 and then pick the 3rd item in the &hellip;<\/p>\n","protected":false},"author":29,"featured_media":201,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unix: Split string using separator - System Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unix: Split string using separator - System Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\" \/>\n<meta property=\"og:site_name\" content=\"System Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/systemcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-22T14:15:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mark Needham\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@systemcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@systemcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mark Needham\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\"},\"author\":{\"name\":\"Mark Needham\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/ae1c58c8f59ab2d33fc598b0ad933eb4\"},\"headline\":\"Unix: Split string using separator\",\"datePublished\":\"2016-06-22T14:15:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\"},\"wordCount\":342,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg\",\"articleSection\":[\"Unix\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\",\"name\":\"Unix: Split string using separator - System Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg\",\"datePublished\":\"2016-06-22T14:15:56+00:00\",\"description\":\"I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an\",\"breadcrumb\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.systemcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unix\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/unix\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Unix: Split string using separator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"name\":\"System Code Geeks\",\"description\":\"Operating System Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/systemcodegeeks\",\"https:\/\/x.com\/systemcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/ae1c58c8f59ab2d33fc598b0ad933eb4\",\"name\":\"Mark Needham\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g\",\"caption\":\"Mark Needham\"},\"sameAs\":[\"http:\/\/www.markhneedham.com\/blog\/\"],\"url\":\"https:\/\/www.systemcodegeeks.com\/author\/mark-needham\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unix: Split string using separator - System Code Geeks - 2026","description":"I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/","og_locale":"en_US","og_type":"article","og_title":"Unix: Split string using separator - System Code Geeks - 2026","og_description":"I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an","og_url":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/","og_site_name":"System Code Geeks","article_publisher":"https:\/\/www.facebook.com\/systemcodegeeks","article_published_time":"2016-06-22T14:15:56+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg","type":"image\/jpeg"}],"author":"Mark Needham","twitter_card":"summary_large_image","twitter_creator":"@systemcodegeeks","twitter_site":"@systemcodegeeks","twitter_misc":{"Written by":"Mark Needham","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#article","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/"},"author":{"name":"Mark Needham","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/ae1c58c8f59ab2d33fc598b0ad933eb4"},"headline":"Unix: Split string using separator","datePublished":"2016-06-22T14:15:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/"},"wordCount":342,"commentCount":0,"publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg","articleSection":["Unix"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/","url":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/","name":"Unix: Split string using separator - System Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg","datePublished":"2016-06-22T14:15:56+00:00","description":"I recently found myself needing to iterate over a bunch of \u2018\/\u2019 separated strings on the command line and extract just the text after the last \u2018\/\u2019. e.g. an","breadcrumb":{"@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#primaryimage","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/unix-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.systemcodegeeks.com\/unix\/unix-split-string-using-separator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.systemcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Unix","item":"https:\/\/www.systemcodegeeks.com\/category\/unix\/"},{"@type":"ListItem","position":3,"name":"Unix: Split string using separator"}]},{"@type":"WebSite","@id":"https:\/\/www.systemcodegeeks.com\/#website","url":"https:\/\/www.systemcodegeeks.com\/","name":"System Code Geeks","description":"Operating System Developers Resource Center","publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.systemcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.systemcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/systemcodegeeks","https:\/\/x.com\/systemcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/ae1c58c8f59ab2d33fc598b0ad933eb4","name":"Mark Needham","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5489baed26ce2d932bf951ecfb47afe80bec45d3648c23521d87c83b8f1c3ea9?s=96&d=mm&r=g","caption":"Mark Needham"},"sameAs":["http:\/\/www.markhneedham.com\/blog\/"],"url":"https:\/\/www.systemcodegeeks.com\/author\/mark-needham\/"}]}},"_links":{"self":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/1441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/comments?post=1441"}],"version-history":[{"count":0,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/1441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media\/201"}],"wp:attachment":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media?parent=1441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/categories?post=1441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/tags?post=1441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}