first bash script not working and no clue how to find out why?

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
sandbagger
Level 3
Level 3
Posts: 187
Joined: Mon Dec 28, 2020 12:36 pm

first bash script not working and no clue how to find out why?

Post by sandbagger »

Hi all
all I want to do is run a command from anywhere which takes me to a specific directory without typing it in. I read HOW to do it and for some reason it isn't working and I don't know why. The script looks like this

Code: Select all

#!/bin/bash
cd /home/dave/j_sandbox/j_raw/
I chmod +x it and then cp'd it to a directory on my search path

Code: Select all

dave@deepthought:~/.local/bin$ ls -l
-rwxrwxr-x 1 dave dave  45 Jan 20 09:54 gotoj_raw.sh
when I run it nothing happens

Code: Select all

dave@deepthought:~/.local/bin$ gotoj_raw.sh 
dave@deepthought:~/.local/bin$ pwd
/home/dave/.local/bin
dave@deepthought:~/.local/bin$ gotoj_raw.sh 
dave@deepthought:~/.local/bin$ pwd
/home/dave/.local/bin


but when I run the command from the bash it works???

Code: Select all

]
dave@deepthought:~/.local/bin$ cd /home/dave/j_sandbox/j_raw/
dave@deepthought:~/j_sandbox/j_raw$ 

Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: first bash script not working and no clue how to find out why?

Post by dave0808 »

When you run the bash script, it's creating a new shell, running the commands and then exiting. So it is changing directory, but you don't see it. You could add the pwd command as the last line and you'll confirm this.

You can "source" the file, so that it executes within your existing shell. This is done by . <script> - note the 'space' after the period. Generally this isn't done because you can very easily mess up your login session. In your case, it would suffice though. Also, when the script is invoked like this, there's no need for the 'execute' permission on the file.

However, a much better option in this use case, would be to create an alias to invoke the command. Have a look in your .bashrc file and you'll see some examples. New ones should now go into $HOME/.bash_aliases, but the format is the same. Once added, open a new terminal session and just type in the alias that you created.
sandbagger
Level 3
Level 3
Posts: 187
Joined: Mon Dec 28, 2020 12:36 pm

Re: first bash script not working and no clue how to find out why?

Post by sandbagger »

hey there thanks for helping me with this. I thought this was going to be easy but... I didn't see the bash prompt change to reflect the new directory. I did what you said

Code: Select all

 nano $HOME/.bash_aliases
 
dave@deepthought:~$ cat $HOME/.bash_aliases

alias go2j = "cd j_sandbox/j_raw"

and opened a new terminal and entered the alias and it didn't work. What did I miss please. Thank you

Code: Select all

dave@deepthought:~$ go2j
go2j: command not found
dave@deepthought:~$ 

User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: first bash script not working and no clue how to find out why?

Post by JoeFootball »

sandbagger wrote: alias go2j = "cd j_sandbox/j_raw"
I think your formatting is off a bit. Try: alias go2j='cd ~/j_sandbox/j_raw'

Also try closing all other terminals before opening a new one, as I don't know if .bashrc and .bash_alias are re-read if terminal is already up.
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: first bash script not working and no clue how to find out why?

Post by Flemur »

sandbagger wrote: Thu Jan 20, 2022 11:48 pm
I messed around with this issue for a while and someone suggested this...trick? (I don't know what the HOME=. cd part means, but it works!)

Anyway, I go to the /mnt/DATA directory a lot, so...now I just type data to get there:

Code: Select all

$ alias data
data='cd /mnt/DATA;          HOME=. cd'

$ pwd
/home/username

$ data
$ pwd
/mnt/DATA

$ data down*/temp*
$ pwd
/mnt/DATA/download/temp
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
sandbagger
Level 3
Level 3
Posts: 187
Joined: Mon Dec 28, 2020 12:36 pm

Re: first bash script not working and no clue how to find out why?

Post by sandbagger »

JoeFootball wrote: Fri Jan 21, 2022 7:33 am
sandbagger wrote: alias go2j = "cd j_sandbox/j_raw"
I think your formatting is off a bit. Try: alias go2j='cd ~/j_sandbox/j_raw'

Also try closing all other terminals before opening a new one, as I don't know if .bashrc and .bash_alias are re-read if terminal is already up.
hi there
no luck, thanks for taking another look. I must confess, being new to linux, that I thought this would be easy. Ho hum.
thanks again for trying

Code: Select all

dave@deepthought:~$ cat $HOME/.bash_aliases
alias go2j='cd ~/j_sandbox/j_raw'
dave@deepthought:~$ gotoj

Command 'gotoj' not found, did you mean:

  command 'gotox' from deb dvb-apps (1.1.1+rev1500-1.2)

Try: sudo apt install <deb name>

dave@deepthought:~$ 

sandbagger
Level 3
Level 3
Posts: 187
Joined: Mon Dec 28, 2020 12:36 pm

Re: first bash script not working and no clue how to find out why?

Post by sandbagger »

Flemur wrote: Fri Jan 21, 2022 9:55 am
sandbagger wrote: Thu Jan 20, 2022 11:48 pm
I messed around with this issue for a while and someone suggested this...trick? (I don't know what the HOME=. cd part means, but it works!)

Anyway, I go to the /mnt/DATA directory a lot, so...now I just type data to get there:

Code: Select all

$ alias data
data='cd /mnt/DATA;          HOME=. cd'

$ pwd
/home/username

$ data
$ pwd
/mnt/DATA

$ data down*/temp*
$ pwd
/mnt/DATA/download/temp

it worked !!!!!

Code: Select all

dave@deepthought:~$ jraw_a
dave@deepthought:~/j_sandbox/j_raw$ cat $HOME/.bash_aliases
alias go2j='cd ~/j_sandbox/j_raw'
alias jraw_a='cd ~/j_sandbox/j_raw;          HOME=. cd'
dave@deepthought:~/j_sandbox/j_raw$ 

never in a million years would I have found that!! thanks so much. Also thanks to JoeFootball and dave0808 for helping me understand more of this world..
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: first bash script not working and no clue how to find out why?

Post by JoeFootball »

sandbagger wrote: dave@deepthought:~$ cat $HOME/.bash_aliases
alias go2j='cd ~/j_sandbox/j_raw'
dave@deepthought:~$ gotoj

Command 'gotoj' not found, did you mean:
You created the alias go2j, yet you executed gotoj, hence command not found. :)
sandbagger
Level 3
Level 3
Posts: 187
Joined: Mon Dec 28, 2020 12:36 pm

Re: first bash script not working and no clue how to find out why?

Post by sandbagger »

dear zarquon
please make it stop :-) I don't think I should be allowed to use linux any more .
thanks for the heads up
have an EXCELLENT and SAFE 2022 . Thanks for all your help
Locked

Return to “Beginner Questions”