DEV Community

Cover image for Working With Shell
Script Koder
Script Koder

Posted on • Updated on

Working With Shell

What is a shell?

  • Linux shell is a program that allows text-based interaction between the user and the computer

Linux Commands

  • Linux command consists of program name followed by the option and arguments.
  • For ex.
cat -n myfile

# program name: cat
# option: -n
# argument: myfile
Enter fullscreen mode Exit fullscreen mode

Home directory

  • The home directory is a directory that stores user-specific data.
  • this data can be of any type
  • for ex. If there is a user named john, then john's data like images, documents, downloads, etc. data will reside in /home/john/ location

How to know the Home directory

echo $HOME
# This command will read from the environment variable $HOME

echo ~
# This command will expand the tilde symbol to the given user's home directory path
Enter fullscreen mode Exit fullscreen mode

Image description

Directory Management

How to create & remove directories

  • mkdir is a command which is used to create directory
  • rmdir is a command which is used to delete a empty directory
  • example
# to create a directory
mkdir test_directory

# to delete a directory
rmdir test_directory
Enter fullscreen mode Exit fullscreen mode

How to create a chain of directories

  • in order to perform this we use "-p" parameter with mkdir command
  • Example
# We want to create 4 directories each inside of it's parent directory
mkdir -p a/b/c/d
Enter fullscreen mode Exit fullscreen mode

How to navigate through directories

  • To switch directories we use cd command
# Changing to the parent directory
cd ../

# Changing to the home directory
cd /home/user1
Enter fullscreen mode Exit fullscreen mode

How to navigate folder directory in Linux

  • there are two types of path notation in Linux
  • 1. Relative Path
  • 2. Absolute Path

Relative path

  • relative path is defined with respect to the current directory
  • "." represents the current working folder
  • ".." represents the parent directory
  • For example
# to change to the parent directory for the current directory
cd ../

# to run a file/script present in the current directory
bash ./example.sh
Enter fullscreen mode Exit fullscreen mode

Absolute path

  • absolute paths are defined with respect to the root "/" location
  • for example
# navigating to the user1 document folder
cd /home/user1/documents
Enter fullscreen mode Exit fullscreen mode

PUSHD & POPD

  • pushd and popd commands are used to manage the directory stack.
  • Example
# we created 3 directories
mkdir -p a/b/c

# now we navigate to c directory using pushd command, by pushing it's path to directory stack
pushd a/b/c

# this command will take us to the given c directory and we can create a file here
echo "hello world" > test_file.txt

# to navigate to the previous path we can use popd command
# this will bring us to the parent directory
popd 
Enter fullscreen mode Exit fullscreen mode

Top comments (13)

Collapse
 
jimmymcbride profile image
Jimmy McBride

Would love to see something about pushd and popd in addition to cd. They can be very handy tools when you changing directories a lot and need to make a quick escape to where you've previously been! :)

Can't wait to see more!

Collapse
 
nerdflash28 profile image
Script Koder

Thanks for your response, i'll keep updating this page.

Collapse
 
fnabinash profile image
Abinash Sahoo

Why not create a series on Linux and bash covering from basics to advanced topics? šŸ¤”šŸ¤”

Thread Thread
 
nerdflash28 profile image
Script Koder

sure, i'm working on it

Collapse
 
kubernetic profile image
BC

I think an important concept is also where a user's shell is defined.

A user's shell is typically setup when the user is added to the system. Often /bin/bash is set as a default, but macOS now sets /bin/zsh as the default shell for users.

A user could find their default shell by viewing the /etc/passwd file. For example,

root:x:0:0:root:/root:/bin/bash
games:x:12:100:games:/usr/games:/sbin/nologin
Enter fullscreen mode Exit fullscreen mode

In the above example root's default shell is set to the Bash shell. While the games user actually has been set to a nologin shell, preventing anyone from logging into the user. This has certain uses for non-interactive user sessions. We have other shell options that we can use as well. There are lots of varieties and it depends on your OS, or what the administrator has set at account creation.

There are different commands to modify a user's shell. But one way you can do this is with the usermod command:

usermod -s /bin/bash USER

If you want to find a list of available shells on your system, try this command:
cat /etc/shells

Collapse
 
pauljlucas profile image
Paul J. Lucas

There's no such thing as a Linux shell. There are only various shells, e.g., bash, tcsh, zsh, etc. You can use exactly the same shells on FreeBSD, macOS, and others as you can on Linux.

Collapse
 
nerdflash28 profile image
Script Koder • Edited

@pauljlucas Yeah, you are correct mate, thanks for the help, i've updated the title accordingly.

Collapse
 
pauljlucas profile image
Paul J. Lucas

You might want to reconsider the banner image and #linux tag as well.

Collapse
 
pauljlucas profile image
Paul J. Lucas

You still mention "Linux shell....". You ought to replace that with "A shell...."

Collapse
 
manchicken profile image
Mike Stemle

Iā€™m glad to see folks still talking about the terminal. That said, none of this is bash-specific. These will all work in a variety of shells, and I hope folks find whatever shell they prefer and have a great time in the shell.

Collapse
 
pauljlucas profile image
Paul J. Lucas

You mention commands, but never once mention the PATH environment variable or explain that commands like mkdir "live" in certain directories and the way commands work is that the shell searches PATH for a matching command.

Collapse
 
kmsaifullah profile image
Khaled Md Saifullah

In my opinion "how bash works" is also an important topic for a beginners, Don't you think so?

Collapse
 
nerdflash28 profile image
Script Koder

Beginners with no experience with bash, see the black screen and don't know what to write on it.

From my Teaching experience, they are more interested in, How to use it.
not "How it works".

I hope you understood my point, sir.

Thanks for your Feedback though

Some comments may only be visible to logged-in visitors. Sign in to view all comments.