How to alias an export in bash - bash

I am trying to set an env variable which I can use to do relative directory chains. I am trying to do it the following way but cant get it to work. How do I do it?
alias sroot="export SROOT="$PWD""
alias drumit="cd $SROOT/abc/def/drumit"
If I type sroot, it takes the alias but when i type drumit, it gives me an error saying
bash: cd: /abc/def/drumit: No such file or directory
Looks like when the shell was launched it takes $SROOT as .
Appreciate any help.
Thanks

Your $PWD and $SROOT variables are being expanded at the time you define the aliases, not when you are using them. Put a \ in front of them to escape them while they are defined.
alias sroot="export SROOT="\$PWD""
alias drumit="cd \$SROOT/abc/def/drumit"

When you initially set the alias, it expands $PWD instead of keeping it as the variable form. Try using function instead like this:
$ function sroot {
> export SROOT="$PWD"
> }
$ export -f sroot
$ function drumit {
> cd $SROOT/cron
> }
$ export -f drumit
$ declare -f sroot
sroot()
{
export SROOT="$PWD"
}
$ declare -f drumit
drumit ()
{
cd $SROOT/abc/def/drumit
}
This is what is currently happening when you alias like in your question (variable expanding):
$ alias sroot="export SROOT="$PWD""
$ alias drumit="cd $SROOT/abc/def/drumit"
$ alias
alias SROOT='/home/jon'
alias drumit='cd /home/jon/abc/def/drumit'
alias sroot='export SROOT=/home/jon'
Escaping would work too:
$ alias sroot="export SROOT="\$PWD""
$ alias drumit="cd \$SROOT/abc/def/drumit"
$ alias
alias drumit='cd $SROOT/abc/def/drumit'
alias sroot='export SROOT=$PWD'

Related

Why is this something does Bash say that modavar command even tho i aliased it

Note this is sourced, so this is not a shell script.
I am not asking for how to enable alias in noninteractive shell. I did this: shopt -s expand_aliases.
The Bash version is 5.1.4.
How to recreate:
Create a file named "p":
linky(){
comdll="cat"
shopt -s expand_aliases
alias modavar="$comdll"
echo "$argin" | modavar #| getlinks "$argin" | sort -u
}
Then run
echo "source p ; linky https://duckduckgo.com" | bash --norc
Expected output:
https://duckduckgo.com
Actual output:
p: line 5: modavar: command not found
When I run this once it give me
linky(){ alias jkl=echo\ hel; jkl; }
linky
bash: jkl: command not found
But if I do this,
linky(){ alias jkl=echo\ hel; jkl; }
linky
linky(){ alias jkl=echo\ hel; jkl; }
linky
It gives me
hel
What is happening?
You can't define the alias inside a function and use it there. Consider these examples:
alias foo=cat
ffoo() {
echo abc|foo
}
fbar() {
alias bar=cat
echo abc|bar
}
ffoo #->prints abc
fbar #->prints command not found

Difference when executing bash function in an alias

I have a function in my .bash_profile for printing text some pre-written text and copying it to the clipboard.
copyandprint () {
s='\\033[1;32m' #strong
n='\\033[0m' #normal
printf -- "printf -- '$1' | pbcopy;" #pbcopy copies to clipboard in macOS
printf -- "echo -e copied '${s}$1${n}' to clipboard"
}
I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.
alias shrug=$( copyandprint '¯\_(ツ)_/¯')
But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.
alias copydate=$( copyandprint "$(date)" )
The value is generated when the script is run, not when the alias is used.
Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:
copyandprint_live () {
s='\\033[1;32m' #strong
n='\\033[0m' #normal
printf -- "$1" | pbcopy
printf -- "echo -e copied ${s}$1${n} to clipboard"
}
alias copydate_live="\$( copyandprint_live \"\$(date)\" )"
The date is generated at the time the alias is used, rather than at the time the script is executed.
But when I use that function the way I used the other one, it fails:
alias shrug_2=$( copyandprint_live '¯\_(ツ)_/¯')
$ shrug_2
#=> -bash: syntax error near unexpected token `ツ'
And I tried putting double quotes, but that didn't work
alias shrug_3=$( copyandprint_live '"¯\_(ツ)_/¯"')
$ shrug_3
#=> copied 033[1
#=> -bash: 32m¯\_(ツ)_/¯033[0m: No such file or directory
My question is, what's going on here? Why do they need to be so different?
Dispensing with the aliases and using functions makes this a lot easier.
copyandprint () {
printf '%s' "$1" | pbcopy
printf 'copied \033[1;32m%s\033[0m to clipboard\n' "$1"
}
shrug () {
copyandprint '¯\_(ツ)_/¯'
}
copydate () {
copyandprint "$(date)"
}
Functions work alike any other command:
$ foo () { echo hi; }
$ foo
hi
You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...) from executing the command at that time.
alias shrug='$( copyandprint "¯\_(ツ)_/¯")'

Using alias in bash function

I have defined an alias like so:
alias X="path/to/program"
and I have a function defined like this:
doX() { X -flag "$1"; }
I put these in my .bashrc file, and when I open bash, I get a syntax error near unexpected token '-flag'. At this point, the alias has been set, but the function has not, due to this error. If I run
doX() { X -flag "$1"; }
at this point, it works. I have tried putting this into a file and sourcing it after I set the alias in the .bashrc file, but it is giving me the same results.
How can I fix this? Is there a way to define the alias AND the function in the .bashrc so that they are both set when I open bash?
Aliases are not usually available in scripts. If you want to have a function use an alias, consider making the alias itself a function:
X() { path/to/program "$#"; }
doX() { X -flag "$1"; }

Bash, [.bashrc] a function for an alias

[In my .bashrc]
Basically I try to make an alias:
alias e='su -c'
But when I write in a terminal:
~$ e ls -goFha /root
I (obviously) get the error:
su: group oFha does not exist
If $str were replaced by the rest of the command, the herebelow code would work:
alias e='su -c "$str"'
But alias don't work this way. Therefore, I thought to a function.
Replacing $str by the whole argument string, it could be something like:
e () {
"su -c '$str'"
}
How to get the whole argument string in a function?
How would you write my function?
Thanks
Here is another solution:
e() {
su -c "$*"
}
You can try this:
e () {
CMD="$#"
su -c "$CMD"
}

How can i change directory in shell script with variables

this script file name "1sr" and i can work in terminal ". 1sr"
i want to change directory "home/byram/workspace/1/src/com/seri/*"
#!bin/sh
f=$(basename $0 | tr -d "sr")
pth="/home/byram/workspace/$f"
my1=$(ls $pth/src/com/seri)
cd $etc/src/com/seri/$my1
after ". 1sr" command f variable set "bash"
how can i fix it?
I would suggest a function called "prj" to put in your .bashrc:
prj () {
cd /home/byram/workspace/"$1"/src/com/seri
}
Then use it like this
prj 1 # Switch to ...1/src/com/seri
prj 2 # Switch to ...2/src/com/seri
i add in .bashrc this lines:
wr (){
cd /home/byram/workspace/"$1"/w
v1=$(ls /home/byram/workspace/"$1"/src/*/*)
v2=$(ls /home/byram/workspace/"$1"/src/*)
v3=$(ls /home/byram/workspace/"$1"/src/)
echo "$v3.$v2.$v1"
}
works for any project eg. com.example.abc,org.samp.xyz
thanks for #chepner

Resources