Creating a new Linux Command without touching the bashrc - bash

I want to write a shell script that allows me to do this in the bash terminal :
Mycommand [options] [param] .
Is there any way that my shell would recognize Mycommand without having to modify the .bashrc ? ( the goal here is to be able to give the script file to someone and they wouldn't have to add anything to their path and still be able to use it as said )

They can run the script by running it from the directory where it is saved:
cd dir/with/script
./script.sh
Or
~/bin/script.sh
If the script is saved in the bin directory in the home of the user.

Define a shell alias. For example, the following alias is called x and it executes ls -alF *.py:
$ alias x="ls -alF *.py"
No files need to be written in order for a shell alias to be defined or used.
Now you can type x into a shell prompt in the shell where you defined the alias, and it will return a detailed list of your Python source files.
$ alias x="ls -alF *.py"
$ x
-rwxrwxr-x 1 mslinn mslinn 653 Feb 19 17:19 django-admin.py*
-rwxrwxr-x 1 mslinn mslinn 1675 Sep 19 2020 jp.py*
-rwxrwxr-x 1 mslinn mslinn 594 Sep 19 2020 rst2html.py*
-rwxrwxr-x 1 mslinn mslinn 6413 Sep 19 2020 wsdump.py*
If you want to be able to specify the pattern for the files to list, do not include that in the alias. For example:
$ alias y="ls -alF"
$ y *.md
-rw-rw-r-- 1 mslinn mslinn 606 Nov 17 20:31 README.md
You can even just define default parameters, and add more:
$ alias z="ls -a"
$ z -lF *.md
-rw-rw-r-- 1 mslinn mslinn 606 Nov 17 20:31 README.md

Your script can call a script within the same folder, to add an alias to the bash session and create variables for your script to run, this would be available for the duration of the bash session. Once you have ended this bash session, the alias and the variables, the script created would be lost.

Related

Having trouble with declared functions in `~/.profile` not being loaded in integrated terminal shells

I have a .profile which is copied to /home/vscode during my container build before running common-debian.sh
This is important as during container image build common-delian.sh will skip creating a default .profile if the file is already present
In that profile I have a number of bash functions and some exported variables.
One of the variables relies on executing one of the declared functions, and in my shell I can see the correct variable and value is set.
This problem arises when opening a new integrated terminal in vscode.
"new terminal" command
However none of my functions are available in the shell to execute.
Confirmed through typeset -F
I can run source ~/.profile and the functions are then available in the shell.
Is there some place I can turn up logging to determine what is happening here?
Details about the install
vscode Host install is Mac M1
VERSION='0.202.6'
CONTENTS_URL='https://github.com/microsoft/vscode-dev-containers/tree/main/containers/debian/history/0.202.6.md'
DEFINITION_ID='debian'
VARIANT='bullseye'
GIT_REPOSITORY='https://github.com/microsoft/vscode-dev-containers/'
GIT_REPOSITORY_RELEASE='v0.224.0'
BUILD_TIMESTAMP='Fri, 25 Feb 2022 10:48:36 GMT'
Example function declaration
kconfig() {
ls $HOME/.kube/configs/* | tr '\n' ':'
}
declare -f kconfig
Example variable making use
export KUBECONFIG="$(kconfig)"
I can confirm before sourcing .profile, this variable is set and valid and should only have come from the .profile declaration.
home folder for reference
vscode ➜ /workspace (master ✗) $ ls -al ~
total 76
drwxr-xr-x 1 vscode vscode 4096 Apr 27 18:59 .
drwxr-xr-x 1 root root 4096 Feb 25 10:55 ..
drwxr-xr-x 1 501 dialout 160 Mar 8 22:31 .aws
-rw-r--r-- 1 vscode vscode 220 Aug 4 2021 .bash_logout
-rw-r--r-- 1 vscode vscode 4665 Feb 25 10:55 .bashrc
drwxr-xr-x 3 vscode vscode 4096 Apr 27 18:59 .cache
drwxr-xr-x 1 501 dialout 192 Apr 25 21:41 .config
drwxr-xr-x 1 501 dialout 160 Apr 27 14:20 .creds
-rw-r--r-- 1 vscode vscode 500 Apr 27 18:59 .gitconfig
drwx------ 2 vscode vscode 4096 Apr 27 18:59 .gnupg
drwxr-xr-x 1 501 dialout 160 Mar 9 21:59 .kube
drwxr-xr-x 12 vscode vscode 4096 Feb 25 10:55 .oh-my-zsh
-rw-r--r-- 1 vscode vscode 2381 Apr 27 19:19 .profile
drwxr-xr-x 1 vscode root 4096 Apr 27 18:59 .vscode-server
drwxr-xr-x 3 vscode root 4096 Apr 27 18:26 .vscode-server-insiders
-rw-r--r-- 1 vscode vscode 3897 Feb 25 10:55 .zshrc
Edit below:
Now I am copying this file in as ~/.bash_profile
There is still a .profile, vscode common-debian.sh creates this if the file is not present when building the container image.
vscode ➜ /workspace (master ✗) $ ls -al ~
total 80
drwxr-xr-x 1 vscode vscode 4096 Apr 27 20:08 .
drwxr-xr-x 1 root root 4096 Feb 25 10:55 ..
drwxr-xr-x 1 501 dialout 160 Mar 8 22:31 .aws
-rw-r--r-- 1 vscode vscode 220 Aug 4 2021 .bash_logout
-rw-r--r-- 1 vscode vscode 2671 Apr 27 19:57 .bash_profile
-rw-r--r-- 1 vscode vscode 4665 Feb 25 10:55 .bashrc
drwxr-xr-x 3 vscode vscode 4096 Apr 27 20:08 .cache
drwxr-xr-x 1 501 dialout 192 Apr 25 21:41 .config
drwxr-xr-x 1 501 dialout 160 Apr 27 14:20 .creds
-rw-r--r-- 1 vscode vscode 500 Apr 27 20:08 .gitconfig
drwx------ 2 vscode vscode 4096 Apr 27 20:08 .gnupg
drwxr-xr-x 1 501 dialout 160 Mar 9 21:59 .kube
drwxr-xr-x 12 vscode vscode 4096 Feb 25 10:55 .oh-my-zsh
-rw-r--r-- 1 vscode vscode 807 Aug 4 2021 .profile
drwxr-xr-x 1 vscode root 4096 Apr 27 20:08 .vscode-server
drwxr-xr-x 3 vscode root 4096 Apr 27 20:07 .vscode-server-insiders
-rw-r--r-- 1 vscode vscode 3897 Feb 25 10:55 .zshrc
also added the following to /etc/profile.d/
# This file is intended to be copied into /etc/profile.d
project_profile="$HOME/.bash_profile"
if [ -r $project_profile ]; then
source $project_profile
else
echo "File not found or not readable: $project_profile"
fi
none of which has had an effect on the resulting new shells
perhaps ignoring vscode
docker exec -it {container} /bin/bash still has the same results.
I have also tried creating an empty terminal profile with an empty shell profile, launching vscode from that terminal window.
Thinking that perhaps inherited env was causing issues.
Also set the following user setting
"terminal.integrated.inheritEnv": false,
From the man page for bash (in the INVOCATION section):
When bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment, expands
its value if it appears there, and uses the expanded value as the name
of a file to read and execute. Bash behaves as if the following command
were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the filename.
In your use case, i.e. a shell script, no startup file is read. But you can change that by setting the value of BASH_ENV to a file you want to read on startup. Alternately your script can source anything you like when it runs, e.g. source .profile.
Extracted from answer comments:
Original Poster noted that the issue was fixed using this in vscode:
"terminal.integrated.defaultProfile.linux": "bash",
"terminal.integrated.profiles.linux": {
"bash": {
"path": "/bin/bash",
"icon": "terminal-bash",
"args": [ "-l" ]
},
}
(The -l arg to make it a login shell being, probably, the important part?)

Messed up my bash_profile and bashrc files - reset to default?

I think I messed up my .bash_profile and .bashrc file.
Is there any way I can reset them to default?
When I want to access them through the terminal I get: Permission denied.
If I open them using sublime text, my .bashrc is completely empty and my .bash_profile contains the following code:
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH=/usr/local/bin:$PATH
alias python=python3
alias sublime="open -a /Applications/Sublime\ Text.app"
source ~/.profile
Any idea on what I could add/delete to any of the files?
Python does not run properly anymore since I accidentally played around on those files.
You may find the default .bash_profile and .bashrc files in /etc/skel/
[user#server /]$ ls -la /etc/skel
total 28
drwxr-xr-x. 3 root root 78 Jan 3 2019 .
drwxr-xr-x. 131 root root 12288 Nov 7 13:03 ..
-rw-r--r--. 1 root root 18 Oct 30 2018 .bash_logout
-rw-r--r--. 1 root root 193 Oct 30 2018 .bash_profile
-rw-r--r--. 1 root root 231 Oct 30 2018 .bashrc
If it's the case you can get them back:
cp /etc/skel/.bash_profile /etc/skel/.bashrc ~/
Make a backup of your current files before ;)
Just to explain: the content of /etc/skel/ is copied into the home directory of a user when created with adduser.

Set runtimepath for vim in command line alias

I want to set an alias for vim so that when I use vim I want all my personal vim files to be sourced. (I want to do this because the machine I use is used by everyone else, so I don't want to affect others' usage. The alias is also set only when I login into the machine)
alias vim='vim -c "source ~/.dc_dotfiles/.vimrc_dc" --cmd "set rtp+=~/.dc_dotfiles/.vim"'
But this is not working as files in ~/.dc_dotfiles/.vim are not being sourced.
Here are the contents of ~/.dc_dotfiles/.vim folder
~$ ls -lhart .dc_dotfiles/.vim/plugin/
total 20K
-rw-r--r-- 1 veveo veveo 18K Oct 18 17:34 abolish.vim
drwxr-xr-x 3 veveo veveo 19 Oct 18 17:35 ..
drwxr-xr-x 2 veveo veveo 24 Oct 18 17:35 .
When I am using the screen I set a flag that will let know if I am logged in. I added this part to my ~/.vimrc
if !empty($DCSCREENFLAG)
set rtp+=~/.dc_dotfiles/.vim
endif
If the flag is set, it means that I am logged in and using the screen, so go ahead and add the directory to the runtimepath.

How can I check the available shells in Mac OSX?

How can I check all the different shells that I can use in OSX Terminal application?
The default one is bash, and I know zsh because I tried and it worked. I wonder how can I check if there are any more than this two.
The easy way is go to /etc and check the shells file. The content is the list of shells available in Mac OSX.
The included by default are:
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Alternatively, you can check their binaries by going to /bin and recognising them visually. Naturally, you have to know them in order to recognise them.
In MacOS the following command will list the available shells on your system
$ ls -l /bin/*sh
-r-xr-xr-x 1 root wheel 618448 Nov 19 00:26 /bin/bash
-rwxr-xr-x 1 root wheel 380016 Feb 7 16:11 /bin/csh
-r-xr-xr-x 1 root wheel 1287040 Sep 21 00:35 /bin/ksh
-r-xr-xr-x 1 root wheel 618512 Nov 19 00:26 /bin/sh
-rwxr-xr-x 1 root wheel 380016 Feb 7 16:11 /bin/tcsh
-rwxr-xr-x 1 root wheel 610288 Sep 21 00:35 /bin/zsh

does bash script set variables by reference?

I'm trying to get some new aliases set and came across a strange one.
alias l="ls"
alias ls="ls -alGh"
I'd like to shorten the default file list to L and set an all info one to the default, LS, but all I ever get is:
~ Desktop $ unalias ls
~ Desktop $ unalias l
~ Desktop $ source ~/.bash_aliases
~ Desktop $ l
total 8
drwxr-xr-x 3 astagl staff .
drwxr-xr-x 34 astagl staff ..
-rw-r--r-- 1 astagl staff 2015-06-07_10.50.54.inline editor.html
~ Desktop $ ls
total 8
drwxr-xr-x 3 astagl staff .
drwxr-xr-x 34 astagl staff ..
-rw-r--r-- 1 astagl staff 2015-06-07_10.50.54.inline editor.html
~ Desktop $
Directory listing above: I'm clearing out the aliases first, just to start over and source my alias script which includes the first listed code block. Is there some kind of weird variable referencing going on here?
Solution
using the backsplashes seems to do the trick:
alias l="\ls -a"
alias ls="\ls -al"
When expanding an alias, other aliases may be invoked recursively. To avoid that you need to escape the commands in the aliases using a backslash:
alias l="\ls"
alias ls="\ls -alGh"

Resources