How to replace ~ with /Users/$USER in a shell script [duplicate] - bash

This question already has an answer here:
Bash path variable using ~ resulting in 'No such file or directory'
(1 answer)
Closed 7 years ago.
In a bash script, I want the user to enter a path to an application, and then launch that application. By default, the application is expected to be in the user's own directory. The following script works for me on Mac OS X, but it is not particularly elegant:
path_default="~/neo4j/bin/neo4j"
read -p "Enter path to neo4j [$path_default]: " path
path="${path:-$path_default}"
if [[ ${path:0:1} == "~" ]]; then
path="/Users/$USER"${path#"~"}
fi
$path start
How can I improve this so that it will work on any platform?

if you consider ~ can be in any platform you can use a variable, for example userpath=$(echo ~)

Try this:
path_default="$HOME/bin/neo4j"
read -p "Enter path to neo4j [$path_default]: " path
path="${path:-$path_default}"
path="${path/#\~/$HOME}"
Here, tilde in the beginning of the path variable is substituted by the contents of $HOME variable.

Related

How to cd to path in a variable in bash script [duplicate]

This question already has an answer here:
Why is a tilde in a path not expanded in a shell script?
(1 answer)
Closed 10 months ago.
Im trying to do cd to a path that is in variable called path. But cannot cd into it.
$repo="test"
path="~/code/forks/$repo/client"
echo $path #echoing correct working path
cd "$path"
pwd
The quotes are preventing the shell from expanding ~ to your home directory. (~ is a shell feature, not an actual directory name.)
path=~/code/forks/$repo/client
This expands ~ during the assignment, not when the parameter is expanded.

How to dynamically compute value of a variable and use it in a command? [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 6 years ago.
Reducing what I'm trying to do to a simple example:
env temp=`pwd` echo "$temp"
I get this message:
temp: Undefined variable.
How do I make it work (in a shell-agnostic way)? I'm expecting the result of pwd to be printed.
My actual requirement uses a complicated expression in place of pwd, a script in place of "echo", and the variable $temp as an argument to that script.
Also, I want to set this variable only for this single command, and not for the whole shell (or any subsequent subshells).
How about something like this for sh and bash
(export temp=`pwd`; echo $temp)
And this for csh
csh -c 'setenv temp `pwd`; echo "$temp"'

Bash Script Requires Enter Before Any Input [duplicate]

This question already has answers here:
Script parameters in Bash
(5 answers)
Closed 6 years ago.
I've written a simple bash script, named ocropus:
#!/bin/bash
read filename path
...
And then i realized that I can't run it like this:
ocropus filename path
Instead, I need to run it like this:
ocropus
filename path
What can I do so I don't need to hit enter before my inputs? Thanks a lot!
Command line arguments are in $1, $2, etc. So do:
filename=$1
path=$2
instead of
read filename path

Search for PATH= [duplicate]

This question already has answers here:
where is $PATH set? Specifically where is my mac port path being set? [closed]
(2 answers)
Closed 6 years ago.
I used a program that set system setting and I can't find where my PATH environment variable was set, it's not set in:
.bashrc
.bash_profile
.profile
etc/paths
I have been trying to use a grep command:
grep -rl "PATH=" /
but the command is taking forever.
Does anyone have any suggestion on searching for where my PATH was assigned?
Run the following command:
PS4='+ $BASH_SOURCE:$LINENO:' BASH_XTRACEFD=7 bash -xlic "" 7>trace.out
Now, look through the file trace.out. It will show you everytime PATH was modified along with the file name and line number which caused the change.
Example
$ grep PATH trace.out
+ /etc/profile:7:PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
+ /etc/profile:9:export PATH
This tells you that line 7 of the file /etc/profile set the PATH.
How it works
The short story is that this starts up a bash login session with tracing turned on. For a longer explanation, see here.

Why does appending ./ to a file execute the program while just typing the program does not [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 9 years ago.
To be more specific, what I mean is:
Suppose I have a python script called test.py with a proper shebang on the first line, say
#!/usr/bin/env python
print 'this works'
In the shell, when I type:
$ test.py
Nothing happens, but when I type:
$ ./test.py
The command is invoked and prints "this works".
Why is that? I thought "." just meant the current directory. Why would having a ./ suddenly mean we want to execute whatever comes next? Thanks.
The shell only looks for executables in the path (try echo $PATH in the shell to see its value). If . is not in the path, you'll have to give the explicit path to the executable. Not having . in the path is a good idea: imagine somebody managed to put an executable named ls in your home directory!
Because the current directory (where you had saved test.py) is not in your $PATH environment variable.
$PATH is searched for executables when you try to run one.

Resources