bash error message seemingly comes from environment instead of from bash itself when executing inside a function

Consider the following simple bash script

cd nowhere

This results in an error message something like

my-script.sh: line 1: cd: nowhere: No such file or directory

So far so good. This makes sense. Let’s tweak it a bit, by wrapping the cd in a function:

function go_nowhere { cd nowhere; }
go_nowhere

We get the same error message. This still makes sense. Let’s try a subshell instead of using a function.

bash -c 'cd nowhere'

Again, similar error message, but now bash “signs” the error message in its own name instead of our script’s name:

bash: line 1: cd: nowhere: No such file or directory

So far, everything still makes sense. Now let’s do both! We’ll wrap cd in a function, and run that function in a subshell:

function go_nowhere { cd nowhere; }
export -f go_nowhere
bash -c 'go_nowhere'

Now are message is a bit different. First, the line number is “line 0” instead of line 1. This makes sense: since we used “export”, that means from the perspective of the subshell, the function was defined “before the first line” since it was inherited from its parent. But the other difference is weirder:

environment: line 0: cd: nowhere: No such file or directory

What is environment? I am not running any program called “environment” so why is bash printing an error message that claims to be coming from such a program? Is it trying to tell me that the error is in a function, which bash inherited from the environment? We can test that theory:

function go_nowhere { cd nowhere; }
bash -c "`declare -f`; go_nowhere"

The line number is now bigger again, but the error message is still “signed” as environment:

environment: line 3: cd: nowhere: No such file or directory

Now I really don’t get it. It did NOT get this function from the environment. So where is the string environment coming from? Note that this is not specific to scripts… this can be reproduced interactively from the prompt: an error inside a function in a subshell gets reported as from environment and I have no idea why

3

What is environment?

It is a hardcoded string in bash source code here: https://github.com/bminor/bash/blob/master/make_cmd.c#L804 .

  /* Assume that shell functions without a source file before the shell is
     initialized come from the environment.  Otherwise default to "main"
     (usually functions being defined interactively) */
  if (temp->source_file == 0)
    temp->source_file = shell_initialized ? "main" : "environment";

Do not use backticks. Prefer $(..). Check your code with shellcheck.

You can get the other string "main" by reading from stdin. shell_initialized means the shell is ready to read interactive input.

$ LC_ALL=C bash -s <<<"$(declare -f go_nowhere); go_nowhere"
main: line 3: cd: nowhere: No such file or directory

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật