Home Why isn't echo "$0" printing the script name in bash?
Reply: 0

Why isn't echo "$0" printing the script name in bash?

user6916 Published in September 19, 2018, 9:14 am

This question already has an answer here:

  • Reliable way for a bash script to get the full path to itself? [duplicate] 23 answers

In school I learned that "$0" would be the name of the script in bash, but when I try to print it, it actually prints -bash instead of the scriptname.

echo "$0"

Output: -bash

Is there something I missed, or is there another command to get the name of the script?

share|improve this question

marked as duplicate by Charles Duffy bash StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var $hover = $(this).addClass('hover-bound'), $msg = $hover.siblings('.dupe-hammer-message'); $hover.hover( function() { $hover.showInfoMessage('', { messageElement: $msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Apr 16 at 15:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Use $BASH_SOURCE, not $0. $0 is generally unreliable, as described in BashFAQ #28. – Charles Duffy Apr 16 at 15:04

2 Answers 2

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.358192 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO