Reputation: 21
So I've been starting bash scripting, and I'm making a script that will autoinstall from source (so essentially it just compiles tarballs for you). I need it to change directories in order to go to a tarball. However, whenever I use this command
read path
cd $path
I always get the error tar-installer.sh: line 13: cd: ~: No such file or directory
For anyone needing it, here's the full script...
#!/bin/bash
# This program auto-installs tarballs for you.
# I designed this for Linux noobies who don't
# know how to install tarballs. Or, it's for
# people like me who are just lazy, and don't
# want to put in the commands ourselves.
echo "Tar Installer v1.1"
echo "Gnu GPL v2.1"
echo -n "Path to tarball:"
read path
cd $path
echo -n "Please enter the file you wish to complile..."
read file
if $file =="*.tar.gz"
then
tar -xzf $file
else
$file =="*.tgz"
then
tar -xzf $file
else
$file =="*.tar.bz2"
then
tar -xjf $file
The final part with the tarballs is still a work in progress. But the directory I used for cd path
was ~/Downloads/
It's probably an easy fix, but I don't know how to fix it.
Upvotes: 1
Views: 5307
Reputation: 316
Because "cd" is build-in function of bash. So you should try to run your script like this:
#source tar-installer.sh
Similar issue here:
[1]: https://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script
Upvotes: -2
Reputation: 3358
You need to replace the tilde ~
by the home path. This expansion fails if it is not performed directly.
cd "${path/#~/$HOME}"
will replace ~
with your home directory using bash's replace ${value/search_term/replacement}
.
You may also want to combine echo & read:
read -p "Path to tarball: " pathname
Also be careful when naming variables like path (PATH is your environment variable)
Upvotes: 6