Shell programming or shell scripting consists of most of the features that modern programming languages have to offer. Any script, simple to complex, can be developed using Shell scripting. It is a series of UNIX commands written to accomplish a particular task using a plain text file. Tasks of day-to-day life can be automated with the help of shell scripting. Â.
If you are new to the concepts of Unix, shell, and Perl, read it here. If you know how to use shell scripting and are going to be interviewed for a job that involves it, this article with shell scripting interview questions and answers will help you prepare.
The Bash RC file, also known as bashrc, is a crucial configuration script that runs whenever you start a new Bash shell session It allows customizing your Bash environment by setting aliases, functions, environment variables, and more.
Knowing how to work with the Bash RC file is an essential skill for any Linux power user or system administrator. It comes up frequently during technical interviews for shell scripting, DevOps, SRE, and infrastructure engineering roles.
In this comprehensive guide, I’ll walk you through some of the most common and insightful Bash RC file interview questions you’re likely to encounter this year. From basic concepts to real-world troubleshooting scenarios, these questions will help you master this topic and ace your next big interview
Frequently Asked Bash RC File Interview Questions
Here are some of the most popular Bash RC file interview questions candidates can expect in 2023:
Q1. What is the purpose of the .bashrc file?
The .bashrc file runs whenever you start a new non-login interactive Bash shell. It initializes your Bash environment by setting aliases, functions, environment variables, and more. This allows you to customize the shell.
Q2. What is the difference between .bashrc and .bash_profile?
-
.bashrc runs for every non-login interactive shell session.
-
.bash_profile runs only for login shells like when you first ssh into a server.
-
.bashrc has individual customizations like aliases and prompts.
-
.bash_profile has more global settings like environment variables.
Q3. How do you reload changes made to .bashrc?
Either restart your terminal or run:
source ~/.bashrc
This reloads .bashrc without starting a new session.
Q4. How do you create an alias in .bashrc?
Add a line like this at the end of .bashrc:
alias aliasname="command"
For example:
alias ll="ls -l"
Q5. How do you set an environment variable in .bashrc?
Use the export command:
export VARNAME="value"
For example:
export EDITOR=/usr/bin/vim
Q6. What does “source” mean for a file like .bashrc?
Sourcing a file executes its contents in the current shell. Changes made in the file are reflected in the current session.
Q7. What precautions should you take when editing .bashrc?
- Backup .bashrc before editing
- Avoid changing default paths
- Test changes in a new shell before reloading
- Be cautious with aliases overriding commands
Q8. How can you debug issues in .bashrc?
- Comment out sections and reload to isolate problems
- Check for syntax errors
- Print variables and test functions piece-by-piece
- Use bash debugging options like -x and -v
Intermediate Bash RC File Interview Questions
Let’s look at some more complex and in-depth Bash RC file questions:
Q9. How can you modify .bashrc to change the default prompt?
Change the PS1 variable. For example:
PS1="u@h w> "
Shows username, hostname, cwd.
Q10. How do you use .bashrc to execute initialization commands on SSH login?
Add the following to .bashrc:
if [-n "$SSH_CLIENT"]; then # commands here run on SSH loginfi
This checks if SSH_CLIENT is set.
Q11. What is the proper way to add directories to PATH in .bashrc?
Append new paths instead of replacing PATH:
export PATH="$PATH:/my/new/path"
Q12. How can you customize Terminal appearance and behavior via .bashrc?
- PS1 – Prompt string
- Colors via ANSI escape codes
- Aliases
- Environment variables
Q13. What is the best way to source utility scripts from .bashrc?
Use . (dot) instead of source so the script has its own scope:
. ~/myscripts.sh
Q14. How can you break .bashrc into multiple files?
Use . (dot) to source additional .bashrc files:
. ~/.extrabashrc
Keep separates files for aliases, functions, etc.
Q15. What is the proper location for system-wide bash settings?
/etc/bash.bashrc
Or files under /etc/profile.d/ for login shells.
Advanced Bash RC File Interview Questions
Finally, let’s go through some advanced .bashrc questions:
Q16. How can you trigger commands only on interactive login and not for noninteractive shells?
Check the “-i” flag:
if [ -n $PS1 ] && [ -z $BASH_VERSION ]; then # interactive non-login shellfi
Q17. What is the best practice for checking if a command exists before defining an alias?
Use type -P:
if type -P sudo >/dev/null; then alias s='sudo ' fi
This avoids errors if sudo is not installed.
Q18. How can you debug scripts/commands executed from .bashrc?
Use bash debug mode:
set -x # enable debug logging# commands hereset +x # disable logging
Q19. How do you capture the exit status of a command run from .bashrc?
Save exit code right after the command:
mycmd && ec=$?if [ $ec -eq 0 ]; then # success casefi
Q20. How can you log informative messages from .bashrc without cluttering the terminal?
Redirect output to /dev/null:
echo "Sourcing .bashrc" >/dev/null
This hides message output.
Summary
Being able to handle Bash RC file interview questions demonstrates your expertise with customizing Bash environments. Mastering topics like aliases, functions, debugging techniques, and interactive vs. non-interactive shells shows you have the skills to write robust Bash scripts and configurations.
Use this guide to brush up on both basic and advanced .bashrc concepts. Understand not just how to use features, but also why certain approaches are preferred. This will help you answer Bash RC file questions confidently while showcasing your in-depth knowledge.
Name commonly used shells on a typical Linux system.
There are mainly two kinds of Linux shells- C-shell and Bourne shell. Their derivatives are as follows:
- C-shell: TENEX C-Shell, Z-Shell
- Bourne shell: Korn Shell, POSIX Shell, Bourne-Again Shell
How can you connect to a database server?
For Linux users, the isql tool that comes with the open client driver can be used to connect to a database server. Here is how:
isql âS serverName âU username âP password