{"id":1060,"date":"2008-04-02T18:46:00","date_gmt":"2008-04-02T18:46:00","guid":{"rendered":"https:\/\/isplotchy.com\/blog\/?p=1060"},"modified":"2013-05-04T01:42:24","modified_gmt":"2013-05-04T01:42:24","slug":"unix-script-goodness-and-variable-prefixsuffix-stripping-fun","status":"publish","type":"post","link":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/","title":{"rendered":"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun"},"content":{"rendered":"<p><a href=\"https:\/\/isplotchy.com\/images\/blog\/squirrel.jpg\"><img decoding=\"async\" src=\"https:\/\/isplotchy.com\/images\/blog\/squirrel.jpg\" border=\"0\" alt=\"\"><\/a><br \/>Here&#8217;s something you probably have no interest in, but it&#8217;s a script I wrote to help me with UNIX shell programming that I engage in from time to time.<\/p>\n<p>There&#8217;s a nice way of easily taking a piece of text (often called a &#8220;String&#8221; by programmer-types) and stripping off pieces of it, either from the front or the back, using variable evaluation.<\/p>\n<p>One thing to note about UNIX is that are many, many different ways to do the same thing.  This is just one little feature of UNIX I like to use.<\/p>\n<p>I wrote a script to help me when I want to do this prefix\/suffix stripping kind of variable evaluation.<\/p>\n<p>Here is the script in its entirety:<\/p>\n<p><span>function showUsage {<br \/>   print<br \/>   print &#8216;USAGE:&#8217;<br \/>   print &#8216;ksh -f variableTest.sh <string to test><pattern to strip string variable>&#8216;<br \/>   print<br \/>   print &#8216;=============================================================================================================&#8217;<br \/>   print &#8216;${variable#pattern} evaluates variable, but removes the smallest portion of its prefix which matches pattern.&#8217;<br \/>   print &#8216;${variable##pattern} evaluates variable, but removes the largest portion of its prefix which matches pattern.&#8217;<br \/>   print &#8216;${variable%pattern} evaluates variable, but removes the smallest portion of its suffix which matches pattern.&#8217;<br \/>   print &#8216;${variable%%pattern} evaluates variable, but removes the largest portion of its suffix which matches pattern.&#8217;<br \/>   print &#8216;=============================================================================================================&#8217;<br \/>   print<br \/>   print &#8216;Special instructions:&#8217;<br \/>   print &#8216;======================&#8217;<br \/>   print &#8216;To stop the shell from interpreting wildcards you may use for patterns,&#8217;<br \/>   print &#8216;run this script like the following:&#8217;<br \/>   print<br \/>   print &#8216;ksh -f variableTest.sh <string to test><pattern contained in single quotes>&#8216;<br \/>   print<br \/>   print EXAMPLE: ksh -f variableTest.sh aabbcc \\&#8217;a*\\&#8217;<br \/>   print<br \/>   print<br \/>   exit 1<br \/>}<\/p>\n<p>clear<\/p>\n<p>if [ $# -ne 2 ]<br \/>then<br \/>   showUsage<br \/>fi<\/p>\n<p>variable=$1<br \/>pattern=$2<\/p>\n<p>print<br \/>print &#8216;=============================================================================================================&#8217;<br \/>print &#8216;${variable#pattern} evaluates variable, but removes the smallest portion of its prefix which matches pattern.&#8217;<br \/>print &#8216;${variable##pattern} evaluates variable, but removes the largest portion of its prefix which matches pattern.&#8217;<br \/>print &#8216;${variable%pattern} evaluates variable, but removes the smallest portion of its suffix which matches pattern.&#8217;<br \/>print &#8216;${variable%%pattern} evaluates variable, but removes the largest portion of its suffix which matches pattern.&#8217;<br \/>print &#8216;=============================================================================================================&#8217;<br \/>print<br \/>print &#8216;variable: &#8216; $variable<br \/>print &#8216;pattern:  &#8216; $pattern<br \/>print<br \/>print &#8216;${variable#pattern}       &#8216; ${variable#$pattern}<br \/>print &#8216;${variable##pattern}      &#8216; ${variable##$pattern}<br \/>print &#8216;${variable%pattern}       &#8216; ${variable%$pattern}<br \/>print &#8216;${variable%%pattern}      &#8216; ${variable%%$pattern}<\/pattern><\/string><\/pattern><\/string><\/span><\/p>\n<p>Most of this script is just printing stuff out to the screen.  There&#8217;s a whole big piece of code that just tells you how to run the script.<\/p>\n<p>Anyways, say you want to find out what directory you are in on a UNIX file system, and want to save this off in a variable, but without all the nested subdirectories your directory rests in (UNIX is all about the nested subdirectories).<\/p>\n<p>You can use this script to figure out the right pattern to get your current directory minus the path.<\/p>\n<p><span>Example:<\/span><\/p>\n<p>Let&#8217;s say I&#8217;m in:<br \/><span>\/usr\/appl\/abc\/very\/very\/long\/directory<\/span><\/p>\n<p>After some trial and error running my script, I can eventually figure out how to get my current directory, minus the path.<\/p>\n<p><span><br \/>>ksh -f variableTest.sh \/usr\/appl\/abc\/very\/very\/long\/directory &#8216;*\/&#8217;<\/p>\n<p>variable:  \/usr\/appl\/abc\/very\/very\/long\/directory<br \/>pattern:   *\/<\/p>\n<p>${variable#pattern}        usr\/appl\/abc\/very\/very\/long\/directory<br \/>${variable##pattern}       directory<br \/>${variable%pattern}        \/usr\/appl\/abc\/very\/very\/long\/directory<br \/>${variable%%pattern}       \/usr\/appl\/abc\/very\/very\/long\/directory<br \/><\/span><\/p>\n<p>Your current working directory (including the path) is stored in a variable called $PWD.<\/p>\n<p>So, to get your current working directory only in a script you are writing, you can just write the following line:<\/p>\n<p><span><br \/>MY_DIRECTORY=${PWD##*\/}<br \/><\/span><\/p>\n<p>Why did I write a script to do this?  Because I can <span>never<\/span> remember how the pattern matching works, and thought it would be easier to write a script to show me instead.<\/p>\n<p>Now, you&#8217;re probably asking me, &#8220;Splotchy, why would you put something in a program that many, including you, do not fully understand or remember how it works?&#8221;<\/p>\n<p>As <a href=\"http:\/\/lotsasplainin.blogspot.com\/\">Matty Boy<\/a> would say, that&#8217;s a great question, <span>hypothetical question asker<\/span>!<\/p>\n<p>One thing that I neglected to mention about UNIX programming is that it is notoriously squirrelly.  And this feature I am making use of is pretty damned squirrelly, too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s something you probably have no interest in, but it&#8217;s a script I wrote to help me with UNIX shell programming that I engage in from time to time. There&#8217;s a nice way of easily taking a piece of text (often called a &#8220;String&#8221; by programmer-types) and stripping off pieces of it, either from the &hellip; <a href=\"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"episode_type":"","audio_file":"","podmotor_file_id":"","podmotor_episode_id":"","cover_image":"","cover_image_id":"","duration":"","filesize":"","filesize_raw":"","date_recorded":"","explicit":"","block":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14,55,218],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-computers","category-geek","category-unix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun - SPLOTCHY<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun - SPLOTCHY\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s something you probably have no interest in, but it&#8217;s a script I wrote to help me with UNIX shell programming that I engage in from time to time. There&#8217;s a nice way of easily taking a piece of text (often called a &#8220;String&#8221; by programmer-types) and stripping off pieces of it, either from the &hellip; Continue reading UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/\" \/>\n<meta property=\"og:site_name\" content=\"SPLOTCHY\" \/>\n<meta property=\"article:published_time\" content=\"2008-04-02T18:46:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-05-04T01:42:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/splotchy.com\/images\/blog\/squirrel.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"UNIX Script Goodness And Variable Prefix\\\/Suffix Stripping Fun\",\"datePublished\":\"2008-04-02T18:46:00+00:00\",\"dateModified\":\"2013-05-04T01:42:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/\"},\"wordCount\":665,\"commentCount\":8,\"image\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/isplotchy.com\\\/images\\\/blog\\\/squirrel.jpg\",\"articleSection\":[\"computers\",\"geek\",\"unix\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/\",\"url\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/\",\"name\":\"UNIX Script Goodness And Variable Prefix\\\/Suffix Stripping Fun - SPLOTCHY\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/isplotchy.com\\\/images\\\/blog\\\/squirrel.jpg\",\"datePublished\":\"2008-04-02T18:46:00+00:00\",\"dateModified\":\"2013-05-04T01:42:24+00:00\",\"author\":{\"@id\":\"\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#primaryimage\",\"url\":\"https:\\\/\\\/isplotchy.com\\\/images\\\/blog\\\/squirrel.jpg\",\"contentUrl\":\"https:\\\/\\\/isplotchy.com\\\/images\\\/blog\\\/squirrel.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/2008\\\/04\\\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UNIX Script Goodness And Variable Prefix\\\/Suffix Stripping Fun\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/\",\"name\":\"SPLOTCHY\",\"description\":\"jung vf fcybgpul?\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"\",\"url\":\"https:\\\/\\\/isplotchy.com\\\/blog\\\/author\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun - SPLOTCHY","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:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/","og_locale":"en_US","og_type":"article","og_title":"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun - SPLOTCHY","og_description":"Here&#8217;s something you probably have no interest in, but it&#8217;s a script I wrote to help me with UNIX shell programming that I engage in from time to time. There&#8217;s a nice way of easily taking a piece of text (often called a &#8220;String&#8221; by programmer-types) and stripping off pieces of it, either from the &hellip; Continue reading UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun &rarr;","og_url":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/","og_site_name":"SPLOTCHY","article_published_time":"2008-04-02T18:46:00+00:00","article_modified_time":"2013-05-04T01:42:24+00:00","og_image":[{"url":"https:\/\/splotchy.com\/images\/blog\/squirrel.jpg","type":"","width":"","height":""}],"twitter_misc":{"Written by":"","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#article","isPartOf":{"@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/"},"author":{"name":"","@id":""},"headline":"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun","datePublished":"2008-04-02T18:46:00+00:00","dateModified":"2013-05-04T01:42:24+00:00","mainEntityOfPage":{"@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/"},"wordCount":665,"commentCount":8,"image":{"@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#primaryimage"},"thumbnailUrl":"https:\/\/isplotchy.com\/images\/blog\/squirrel.jpg","articleSection":["computers","geek","unix"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/","url":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/","name":"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun - SPLOTCHY","isPartOf":{"@id":"https:\/\/isplotchy.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#primaryimage"},"image":{"@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#primaryimage"},"thumbnailUrl":"https:\/\/isplotchy.com\/images\/blog\/squirrel.jpg","datePublished":"2008-04-02T18:46:00+00:00","dateModified":"2013-05-04T01:42:24+00:00","author":{"@id":""},"breadcrumb":{"@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#primaryimage","url":"https:\/\/isplotchy.com\/images\/blog\/squirrel.jpg","contentUrl":"https:\/\/isplotchy.com\/images\/blog\/squirrel.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/isplotchy.com\/blog\/2008\/04\/unix-script-goodness-and-variable-prefixsuffix-stripping-fun\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/isplotchy.com\/blog\/"},{"@type":"ListItem","position":2,"name":"UNIX Script Goodness And Variable Prefix\/Suffix Stripping Fun"}]},{"@type":"WebSite","@id":"https:\/\/isplotchy.com\/blog\/#website","url":"https:\/\/isplotchy.com\/blog\/","name":"SPLOTCHY","description":"jung vf fcybgpul?","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/isplotchy.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"","url":"https:\/\/isplotchy.com\/blog\/author\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3gWlt-h6","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":1,"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":3146,"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions\/3146"}],"wp:attachment":[{"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isplotchy.com\/blog\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}