- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Use cd Command in Bash Scripts
The cd command is one of most fundamental commands in Bash shell. It is used to change current working directory to a specified location. This command is particularly useful when navigating through file system, especially in situations where you need to access files or directories in a different location. In this article, we will explore how to use cd command in Bash scripts.
What is cd Command?
Before diving into usage of cd command, it is important to understand what it does. cd command is a built-in command in Bash that is used to change current working directory. When you execute cd command, you are telling shell to change directory that you are currently working in.
The syntax for cd command is as follows −
Here, directory refers to directory that you want to change to. If you do not specify a directory, cd command will take you to your home directory by default.
Using cd Command in Bash Scripts
The cd command can be used in Bash scripts in a variety of ways. In this section, we will explore some of most common ways to use cd command in Bash scripts.
Changing to a Specified Directory
One of most basic uses of cd command is to change current working directory to a specified directory. This is done by simply specifying directory as an argument to cd command. For example, following Bash script changes current working directory to /usr/local/bin directory −
#!/bin/bash cd /usr/local/bin
Changing to a Relative Directory
In addition to changing to a specified directory, cd command can also be used to change to a relative directory. A relative directory is one that is specified relative to current working directory. For example, following Bash script changes current working directory to a directory called scripts, which is located in parent directory of current working directory −
#!/bin/bash cd ../scripts
In this example, .. notation is used to indicate parent directory of current working directory. cd command then changes to scripts directory, which is located in parent directory.
Changing to Previous Directory
Another useful feature of cd command is ability to change to previous directory. This is done by using - (dash) argument with cd command. For example, following Bash script changes to previous directory −
#!/bin/bash cd -
When this script is executed, current working directory is changed to directory that was previously active.
Using Variables with cd Command
Variables can also be used with cd command in Bash scripts. This is useful when you need to change to a directory that is determined dynamically, such as when directory name is provided as a user input. For example, following Bash script prompts user for a directory name and then changes to that directory −
#!/bin/bash echo "Enter directory name:" read dirname cd $dirname
In this script, read command is used to prompt user for a directory name. cd command is then used to change to directory that was entered by user.
Handling Errors with cd Command
When using cd command in a Bash script, it is important to handle errors that may occur. For example, if specified directory does not exist, cd command will fail and script will continue to execute next command in script, which may lead to unexpected results. To handle errors with cd command, you can use conditional statements to check if directory exists before attempting to change to it. For example, following Bash script checks if a directory exists and changes to it only if it does −
#!/bin/bash if [ -d "/usr/local/bin" ] then cd /usr/local/bin else echo "Directory does not exist" fi
In this script, -d flag is used to check if /usr/local/bin directory exists. If it does, cd command changes to directory. If it does not, an error message is displayed.
Tips and Tricks
Here are some tips and tricks to help you use cd command more efficiently in Bash scripts −
Use pwd command to print current working directory. This can be useful for debugging and for verifying that cd command has changed to correct directory.
Use pushd and popd commands to temporarily change working directory. pushd command pushes current directory onto a stack and changes to a new directory, while popd command pops top directory from stack and changes to it. This can be useful for temporarily changing to a different directory and then returning to original directory.
Use tab completion to quickly change to a directory. When typing a directory name, press tab key to autocomplete directory name. This can save time and prevent errors when typing long directory names.
Use relative paths instead of absolute paths whenever possible. Relative paths are easier to read and maintain than absolute paths, and they can be used even if script is run on a different system with a different file system structure.
Avoid using spaces in directory names. If a directory name contains spaces, it must be enclosed in quotes or escaped with a backslash. This can make scripts harder to read and maintain.
Here are some examples of how cd command can be used in Bash scripts −
Change to a directory specified in a configuration file −
#!/bin/bash config_file="/path/to/config.txt" directory=$(grep "directory=" $config_file | cut -d "=" -f 2) cd $directory
In this script, grep command is used to search for line in configuration file that specifies directory. cut command is used to extract directory name from line. cd command then changes to directory.
Change to a directory specified by a command-line argument
#!/bin/bash directory=$1 if [ -d $directory ] then cd $directory else echo "Directory does not exist" fi
In this script, directory name is passed as a command-line argument. script checks if directory exists before changing to it using cd command.
Change to a directory and run a command
#!/bin/bash cd /path/to/directory ./script.sh
In this script, cd command is used to change to a directory before running a script. This ensures that script is executed in correct directory.
In this article, we have explored cd command in Bash scripts. We have discussed several ways to use cd command, including changing to a specified directory, changing to a relative directory, changing to previous directory, using variables with cd command, and handling errors. By mastering cd command, you can navigate through file system and access files and directories with ease.
Kickstart Your Career
Get certified by completing the courseGet Started