OS X bash: dirname

大憨熊 提交于 2019-12-03 12:25:52

What about:

cd "$(dirname "$0")"

That works for me here.

#!/bin/sh
cd "$(dirname "$0")"

What finally worked for me is changing this:

#!/bin/sh
cd `dirname $0`

To this:

#! /bin/zsh
cd "${0:h}"

This also supports file names and file paths containing spaces. Here's where I found it: http://rentzsch.com/unix/locationAwareCommandFiles

Escaping the inner double quotes is unnecessary:

cd "`dirname "$0"`"

But that doesn't get to the root of the problem, which is that somehow the value of $0 appears to be empty, or perhaps something strange. Try changing running your script this way:

bash -x scriptname

This will echo each line, with variables interpolated, before running it. It is very useful for debugging. Also quite helpful are:

-u: abort on attempt to use undefined variable
-e: abort on first error

Hey not sure about this... But is it possible that your

#!/bin/sh 

Points to something that is not bash? What I usually use is:

#!/usr/bin/bash

Pretty new to the whole scripting thing so not sure.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!