def has_cwd(state, dir, incorrect_msg="Your current working directory should be `{{dir}}`. Use `cd {{dir}}` to navigate there."):
    """Check whether the student is in the expected directory.

    This check is typically used before using ``has_expr_output()``
    to make sure the student didn't navigate somewhere else.

    Args:
        state: State instance describing student and solution code. Can be omitted if used with ``Ex()``.
        dir: Directory that the student should be in. Always use the absolute path.
        incorrect_msg: If specified, this overrides the automatically generated message in
                       case the student is not in the expected directory.

    :Example:

        If you want to be sure that the student is in ``/home/repl/my_dir``: ::

            Ex().has_cwd('/home/repl/my_dir')

    """
    expr = "[[ $PWD == '{}' ]]".format(dir)
    _msg = state.build_message(incorrect_msg, fmt_kwargs={ 'dir': dir })
    has_expr_exit_code(state, expr, output="0", incorrect_msg=_msg)
    return state
Check whether the student is in the expected directory.

    This check is typically used before using ``has_expr_output()``
    to make sure the student didn't navigate somewhere else.

    Args:
        state: State instance describing student and solution code. Can be omitted if used with ``Ex()``.
        dir: Directory that the student should be in. Always use the absolute path.
        incorrect_msg: If specified, this overrides the automatically generated message in
                       case the student is not in the expected directory.

    :Example:

        If you want to be sure that the student is in ``/home/repl/my_dir``: ::

            Ex().has_cwd('/home/repl/my_dir')