22
\$\begingroup\$

Inspired by this question on AskUbuntu.

Your job is extremely simple. Take the PATH environment variable (echo $PATH) and export it such that each entry (separated by the : character) is on its own line.

For example, if the PATH is /bin:/usr/bin:/usr/local/bin, your program should output:

/bin
/usr/bin
/usr/local/bin

Your program may not return a leading newline, but it may return a single trailing newline. You do not need to check if the PATH is right, or that the directory exists. Your program should take no input, meaning that your program is responsible for getting the PATH itself. You may safely assume that objects in the PATH do not contain : or newlines. However, spaces are fair game.

Reference implementations are present in the answers to the question above.

Rules

  • This is (obviously) code-golf, so the shortest answer will win the prized green checkmark.
  • The accepted answer will be tested to make sure it's actually legit.
  • Both Windows and *nix entries are accepted.
    • However, if you don't explicitly specify Windows, I will try running it in Linux and fail. (If it's obvious (hi, Batch!), you don't need to specify explicitly.)
  • Only have one solution per answer. If you have both a Windows and *nix version, I will count the shorter one.
  • If two answers have the same length, I will give priority to the one with the higher vote total. If they have the same vote total, I will count the older one. If the time posted is the same, I will choose the one that executes faster. If they execute in the same amount of time, I don't know.

Leaderboard

var QUESTION_ID=96334,OVERRIDE_USER=15422;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • \$\begingroup\$ Ahh! It was hard to visit the AskUbuntu Question without giving some golfing tipps. \$\endgroup\$ – Roman Gräf Oct 16 '16 at 5:41
  • \$\begingroup\$ Some of the (shell) answers seem to assume that the path doesn't contain spaces. Should they be corrected? \$\endgroup\$ – Dennis Oct 16 '16 at 6:08
  • \$\begingroup\$ @Dennis Typically, the path variable shouldn't have spaces, but as they can, they should be corrected. However, we can safely assume that paths themselves won't contain : or a newline. \$\endgroup\$ – Kaz Wolfe Oct 16 '16 at 7:51
  • \$\begingroup\$ Is a function acceptable? \$\endgroup\$ – corvus_192 Oct 16 '16 at 9:24
  • \$\begingroup\$ @corvus_192 Unless the challenge explicitly says otherwise, functions are allowed. \$\endgroup\$ – Dennis Oct 17 '16 at 15:08

33 Answers 33

0
\$\begingroup\$

DOS .COM File, 52 bytes

00000000  31 f6 8b 16 2c 00 8e da  bf 2f 01 b9 05 00 f3 a6  |1...,..../......|
00000010  74 06 f7 d9 f2 ae eb f0  b4 02 ac 3c 3b 75 06 b2  |t..........<;u..|
00000020  0d cd 21 b0 0a 92 08 c0  74 04 cd 21 eb ea c3 50  |..!.....t..!...P|
00000030  41 54 48 3d                                       |ATH=|
00000034

NASM Source:

BITS 16
ORG 0x100

                                ; we assume ax is zeroed on start                      
        xor     si,si
        mov     dx, [0x2c]
        mov     ds, dx          ; PSP:002C points to the environment segment

searchpath:
        mov     di, PATH
        mov     cx, 5
        repe    cmpsb           ; search for PATH= in environment
        je      loop            ; jump if found
        neg     cx
        repne   scasb           ; scan for '\0': skip to the next env var
        jmp     searchpath

loop:   mov     ah, 2           ; WRITE TO STDOUT; AH=02 DL=char
        lodsb                   ; load the next character from PATH
        cmp     al,59
        jne     nosemi
        mov     dl, 0x0d        ; \ if it is a semicolon,
        int     0x21            ; > output '\r',
        mov     al,0x0a         ; / and change it to '\n'
nosemi: xchg    dx,ax           ; 1 byte shorter than mov dl,al
        or      al,al
        je      end             ; if it is '\0', end
        int     0x21
        jmp     loop

end:    ret

PATH: db "PATH="

On some older DOS versions (e.g: 3.3) SI is apparently zeroed at start, so 2 bytes can be saved. Newer DOS versions apparently set SI to 100h.

\$\endgroup\$
0
\$\begingroup\$

Q 20 Characters

If no double quotes in the output the length is 17 chars

q)-1":"vs getenv`PATH;
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
q)
\$\endgroup\$
0
\$\begingroup\$

Tcl, 34 bytes

puts [regsub -all : $env(PATH) \n]

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.