Unix/Linux shells — changing the current working directory (POSIX/Bourne-compatible shells) In a POSIX/Bourne-compatible shell environment (sh, bash, ksh, dash), which built-in command is used to change the current working directory of the shell process?

Difficulty: Easy

Correct Answer: cd

Explanation:


Introduction / Context:
In Unix and Linux, many actions depend on the current working directory. Scripts, relative paths, and shell navigation all assume you know how to move around the filesystem. The question tests which command changes the working directory in POSIX/Bourne-compatible shells, which include sh, bash, ksh, and dash.



Given Data / Assumptions:

  • The shell is POSIX/Bourne-family (sh/bash/ksh/dash), not csh or tcsh.
  • The action required is changing the shell’s own current directory (not launching a new process).
  • We are choosing the standard, portable command.


Concept / Approach:

Changing the shell’s working directory must be done by a built-in because a separate process cannot modify the parent shell’s state. In POSIX shells, the built-in command for this is cd. Although some shells also accept chdir, cd is the standard portable form. The other names shown either do not exist or are not portable built-ins in this shell family.



Step-by-Step Solution:

Identify the shell family: POSIX/Bourne-compatible.Recall the portable command: cd is the built-in to change directories.Confirm that cd /path changes the working directory for the current shell session.Conclude that the correct option is cd.


Verification / Alternative check:

Type type cd or help cd in bash; you will see it is a shell built-in. Running pwd before and after cd will verify the directory change.



Why Other Options Are Wrong:

  • changedir: not a standard command in POSIX shells.
  • chdir: a C library/system call name; not the standard portable Bourne-shell built-in (though some shells accept it, it is not POSIX portable).
  • cdir: not a standard command.
  • None of the above: incorrect because cd is correct.


Common Pitfalls:

Confusing cd with external programs; forgetting that cd with no argument goes to $HOME; assuming chdir works everywhere (it is not portable across all Bourne-like shells).


Final Answer:

cd

More Questions from Unix

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion