Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

How can I retrieve the absolute filename in a shell script on Mac OS X?

Solution 1:

#!/usr/bin/env bash
scriptDir="$(cd "$(dirname "$0")" && pwd -P)"

Solution 2:

I cheat and use perl for this very thing:

#!/bin/bash
dirname=`perl -e 'use Cwd "abs_path";print abs_path(shift)' $0`
echo $dirname

You'd think I'd just write the entire script in perl, and often I do, but not always.

Tags:

Shell

Bash

Mac Osx

Related

Enable SQL Server to backup on remote machines/drives what's a secure way to send passwords over the internet? Why do I get "Permission denied (publickey)" when trying to SSH from local Ubuntu to a Amazon EC2 server? Best way to compare (diff) a full directory structure? How to keep the full path with rsync? How to change owner of mount point Does Windows have a built-in ZIP command for the command line? What is the correct syntax to run cron every 4 hours? Send an email with an attached file using telnet or netcat Determine Location of Inode Usage What is the difference between unlink and rm? Allowing FTP with IPTables

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy