Thank you! For portability beyond bash, $0 may not always be enough. Go to mysite.com/wiki/Main_Page?action=purge to force MediaWiki to regenerate the cached links. Pick your webserver's configuration from this list. No warranty of any kind. I want to use a Bash script as a launcher for another application.

I had success with this when running a script by itself or by using sudo, but not when calling source ./script.shDoesn't work if the script is being sourced from another script.This does not work if the last part of $0 is a symbolic link pointing to an entry of another directory (great clean solution, but this will not work if the file is symlinked.This works perfectly to get the "real" dirname, rather than just the name of a symlink. Discover the possibilities of the /proc directory av Federico Kereki Denne informatikkrelaterte artikkelen er foreløpig kort eller mangelfull , og du kan hjelpe Wikipedia ved å utvide den . The number of possibilities would be 2^(X) where X is the number of bits in the hash. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under Advantage: Short URLs hide some technical details from end-users, which can be desirable for site owners.Among other things, this means they can be kept stable when the underlying software changes. : A Study in Destructive Haunting, 1981. Or the full code snippet at the bottom? "source my/script.sh"I use this all the time in my bash scripts that automate stuff and often invoke other scripts in the same dir. Could anyone explain why it's downvoted?On Linux, realpath is a standard utility (part of the GNU coreutils package), but it is not a bash built-in (i.e., a function provided by bash itself). Free 30 Day Trial your coworkers to find and share information.

Is there any difference using @User9123 I think the accept one is try to be compatible with all popular shell/distro. We aren't using bash primitives only, but I'm okay with that because The above should cover your needs for locating the currently running script on Linux, but if you don't have the Alternatively you can try the following function in bash:This function takes 1 argument.

MediaWiki's default installation path usually looks something like this: or 'source' in which case it will still be whatever script sourced it, or, if from the command line, '-bash' (tty login) or 'bash' (invoked via 'bash -l') or '/bin/bash' (invoked as an interactive non-login shell)I haven't tested it across different systems. And appears to work just as well. One was very close but had a tiny bug that broke it badly; they forgot to wrap the path in quotation marks.Also a lot of people assume you're running the script from a shell so they forget when you open a new script it defaults to your home.This gets it right regardless how or where you run it:So to make it actually useful here's how to change to the directory of the running script:This would work on all platforms and is super clean.This is a slight revision to the solution e-satis and 3bcdnlklvc04a pointed out in This should still work in all the cases they listed.Note that this has to be the first statement in your script.I've compared many of the answers given, and come up with some more compact solutions. If argument has already absolute path, print it as it is, otherwise print I believe I've got this one. All rights reserved. Here you can pike out the code you like to use.

I want to change the working directory to the one where the Bash script is located, so I can operate on the files in that directory, like so: Organismo simulation. @DanDascalescu The first line in your script needs to be All I want to know is, why this way is not good? @konsolebox, what are you trying to defend against? (See above for the difference between MediaWiki's default URL structure with CGI enabled vs without.) Our official guides for short url configuration include recommendations on how to point the root of your site to your wiki using your short url config. I'm late to the party, but I think some will appreciate it being here if they come across this thread. Please use a sensible name for the page, one that fits in with the below names. Anti-Sartre, with an Essay on Camus, 1981.

will still have index.php unless This manifests in 404 page-not-found errors, because the title gets cut off at the special character. To fix this, create a symbolic link in /var/www pointing to /var/lib/mediawiki with the value of $wgScriptPath in LocalSettings.php. If you are on a shared host, you most likely don't. I'd match rather find a way to handle it directly instead of returning an empty SCRIPT_DIR.unless the script was sourced with . The Goblin Universe (con Ted Holiday), 1982. @tvlooy IMO your answer isn't exactly OK as-is either, because it fails when there is a space in the path. One way to do what you are asking would be to just hardcode the data in the This gets the current working directory on Mac OS X 10.6.6:I tried all of these and none worked. Anyone is welcome to create a how-to solution page and list it below. Featured on Meta And were you feeding it any tricky pathnames? I am not sure that all parts exist before and most answers are complex and overloaded. I'm generally a fan of inlining logical conditionals, but what was the specific error that you were seeing in the pushd? @DanDascalescu Using the one-liner?

More over, depending on what you are trying to do, in most cases people want to obtain the directory where the symlink located instead of the directory of the actual source.If you intend to disprove his comment, PROVE that a script CAN access where it's stored with a code example.This is way shorter than the chosen answer. But this solution is the one that works right away at least on Ubuntu, for me! Mobile devices in a Lab. How do I get the path of the directory in which a Bash script is located, inside that script?.

You may need to substitute "type -p $0" to make this work if the command was found on the path.be careful to quote everything to avoid whitespace issues: Nice! These methods require that you have access to the server configuration. A script shouldn't fail in unpredictable ways just because someone, somewhere, did a anyone who lets people sabotage their system in that way shouldn't leave it up to bash to detect such problems... much less hire people capable of making that kind of mistake.