Home > Enterprise >  Run bash script in current directory Linux
Run bash script in current directory Linux

Time:01-13

Why can't I run a bash script in the current directory I'm in? Whenever I run the script the commands are executed in the home directory. The only answers I found are included below. I do use the zsh shell. I don't know if that changes anything. Thanks in advance!

What I have tried so far:


#!/bin/bash

touch test.txt


#!/bin/bash

cd $PWD

touch test.txt


#!/bin/bash

variable = $PWD

cd $variable

touch test.txt


#!/bin/bash

variable= pwd

cd $variable

touch test.txt


#!/bin/bash cd -

touch test.txt


If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.

CodePudding user response:

in bash there are two things to do:

  1. ensure that the shell script file is saved properly and is chmod'd to be an executable.

To do so, save the file (e.g. script.sh) with the code you want, and then run chmod x script.sh to make linux understand that this file is an executable.

  1. call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).

This should execute the file. from there, it's about your code and if you need help there, please update your question.

  • Related