Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to change parent shell directory [duplicate]

Tags:

bash

shell

unix

cd

What I'm trying to do

I've created a shell script that I've added to my $PATH that will download and get everything setup for a new Laravel project. I would like the script to end by changing my terminal directory into the new project folder.

From what I understand right now currently it's only changing the directory of the sub shell where the script is actually running. I can't seem to figure out how to do this. Any help is appreciated. Thank you!

#! /usr/bin/env bash

echo -e '\033[1;30m=========================================='

## check for a directory
if test -z "$1"; then
  echo -e ' \033[0;31m✖ Please provide a directory name'
  exit
fi

## check if directory already exist
if [ ! -d $1 ]; then
  mkdir $1
else
  echo -e ' \033[0;31m✖ The '"$1"' directory already exists'
  exit
fi

# move to directory
cd $1

## Download Laravel
echo -e ' \033[0;32m+ \033[0mDownloading Laravel...'
curl -s -L https://github.com/laravel/laravel/zipball/master > laravel.zip

## Unzip, move, and clean up Laravel
echo -e ' \033[0;32m+ \033[0mUnzipping and cleaning up files...'
unzip -q laravel.zip
rm laravel.zip
cd *-laravel-*
mv * ..
cd ..
rm -R *-laravel-*

## Make the /storage directory writable
echo -e ' \033[0;32m+ \033[0mMaking /storage directory writable...'
chmod -R o+w storage

## Download and install the Generators
echo -e ' \033[0;32m+ \033[0mInstalling Generators...'
curl -s -L https://raw.github.com/JeffreyWay/Laravel-Generator/master/generate.php > application/tasks/generate.php

## Update the application key
echo -e ' \033[0;32m+ \033[0mUpdating Application Key...'
MD5=`date +”%N” | md5`
sed -ie 's/YourSecretKeyGoesHere!/'"$MD5"'/' application/config/application.php
rm application/config/application.phpe

## Create .gitignore and initial git if -git is passed
if [ "$2" == "-git" ]; then
  echo -e ' \033[0;32m+ \033[0mInitiating git...'
  touch .gitignore
  curl -s -L https://raw.github.com/gist/4223565/be9f8e85f74a92c95e615ad1649c8d773e908036/.gitignore > .gitignore

  # Create a local git repo
  git init --quiet
  git add * .gitignore
  git commit -m 'Initial commit.' --quiet
fi

echo -e '\033[1;30m=========================================='
echo -e ' \033[0;32m✔ Laravel Setup Complete\033[0m'

## Change parent shell directory to new directory
## Currently it's only changing in the sub shell
filepath=`pwd`
cd "$filepath"
like image 266
michaelespinosa Avatar asked Dec 06 '12 21:12

michaelespinosa


People also ask

How do I change the parent directory?

To change to the current working directory's parent directory, type cd followed by a space and two periods and then press [Enter]. To change to a directory specified by a path name, type cd followed by a space and the path name (e.g., cd /usr/local/lib) and then press [Enter].

How do I change directory in a bash script?

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.

How do I change shell directory?

In the shell, the command cd - is a special case that changes the current working directory to the previous working directory by exchanging the values of the variables PWD and OLDPWD. Note: Repeating this command toggles the current working directory between the current and the previous working directory.

How do I change the current working directory to its parent in Unix?

To go up one level of the directory tree, type the following: cd .. The special file name, dot dot ( .. ), refers to the directory immediately above the current directory, its parent directory.


2 Answers

You can technically source your script to run it in your parent shell instead of spawning a subshell to run it. This way whatever changes you make to your current shell (including changing directories) persist.

source /path/to/my/script/script

or

. /path/to/my/script/script

But sourcing has its own dangers, use carefully.

(Peripherally related: how to use scripts to change directories)

like image 86
sampson-chen Avatar answered Oct 26 '22 23:10

sampson-chen


This can't be done. Use exec to open a new shell in the appropriate directory, replacing the script interpreter.

exec bash
like image 37
Ignacio Vazquez-Abrams Avatar answered Oct 27 '22 00:10

Ignacio Vazquez-Abrams